我的工作CakePHP的项目。我已删除网址的index.php的使用.htaccess文件,现在我想从URL和放大器删除视图名称;添加一些其它两种不同的参数。假如我选择的国家和放大器;城市那么这两个参数应该出现在URL上选择它们。
我现在面临的问题是作为CakePHP的需要
www.example.com/Controllername/viewname
但我的要求是这样的
www.example.com/Controllername/param1/param2
如果我通过这种方式查找参数1的控制器和参数2为视图。
首先应该是这样的:
www.example.com/Controllername/
解决方案
在你的 APP / routes.php文件
:
// www.example / COM / Controllername
路由器::连接('/ Controllername,
阵列(控制器=>Controllername','行动'=>'指数'));
// www.example.com/Controllername/param1/param2
路由器::连接('/ Controllername /:参数1 /:参数2',
阵列(控制器=>Controllername','行动'=>索引),
阵列(通行证=>阵列(参数1','参数2')));
和控制器:
//设置为null /一个价值prevent缺少参数错误
公共功能指数($参数1 =空,$参数2 = NULL){
//回声$参数1。 ' 和 ' 。 $参数2;
}
在生成链接:
阵列('控制'=>Controllername','行动'=>'指数','参数1'=>'富','参数2'=> '酒吧');
订购事宜。更改 paramX
来任何你想要的,即国家
和镇
请注意,这并不包括: controllername /参数1
- 都应该是present在这个例子中
有其他的方式来实现这一目标。
I am working on cakephp project. I have removed index.php from URL using .htaccess file and now I want to remove view name from URL & add some other two varying parameter. Suppose I choose country & city then these two parameter should appear in URL on selecting them.
The problem I am facing is as cakephp takes
www.example.com/Controllername/viewname
But my requirement is like this
www.example.com/Controllername/param1/param2
If I pass this way It looks for param1 as controller and param2 as view.
Initially should be like:
www.example.com/Controllername/
解决方案
In your APP/routes.php
:
// www.example/com/Controllername
Router::connect('/Controllername',
array('controller'=>'Controllername', 'action'=>'index'));
// www.example.com/Controllername/param1/param2
Router::connect('/Controllername/:param1/:param2',
array('controller'=>'Controllername', 'action'=>'index'),
array('pass' => array('param1', 'param2')));
and your controller:
// set to null/a value to prevent missing parameter errors
public function index($param1=null, $param2=null) {
//echo $param1 . ' and ' . $param2;
}
When generating links:
array('controller'=>'Controllername', 'action'=>'index', 'param1'=>'foo', 'param2'=>'bar');
Order matters. Change paramX
to anything you want i.e. country
and town
note this does not cover: controllername/param1
- both must be present in this example.
There are other ways to achieve this.
上一篇:如何重定向所有请求到一个文件中,除了使用Apache的图像?重定向、图像、文件、Apache
下一篇:的.htaccess时设置500内部服务器错误ExpiresActive错误、服务器、htaccess、ExpiresActive