我正在使用Cakephp 2.x ..我想从url中删除动作或控制器名称...例如我面临的问题是这样的
我的消息控制器上有一个功能名称索引,其中显示所有移动号码
网址是
www.myweb.com/Messages
现在在我的控制器中有第二个功能,其名称是消息,我在其中收到针对手机号码的消息
所以现在我的网址在点击数字后变为
www.myweb.com/Messages/messages/823214
现在我想删除动作名称消息,因为它看起来很奇怪...想要这样的网址
www.myweb.com/Messages/823214
使用Route元素连接路径时,您可能希望将路由元素替换为传递参数。通过使用Router::connect()的第三个参数,您可以定义哪些路径元素也应该作为传递的参数可用:
// SomeController.php
public function messages($phoneNumber = null) {
// some code here...
}
// routes.php
Router::connect(
'/messages/:id', // E.g. /messages/number
array('controller' => 'messages', 'action' => 'messages'),
array(
// order matters since this will simply map ":id"
'id' => '[0-9]+'
)
);
你也可以参考上面给出的链接,希望它对你有用。
如果我可以帮助你,请告诉我。
问题中的示例类似于REST routing,这是一个内置功能,可以映射:
GET /recipes/123 RecipesController::view(123)
要启用休息路由,只需使用Router::mapResources('controllername');
如果您只想为问题中的一个案例写一条路线,则需要使用星型路线:
Router::connect('/messages/*',
array(
'controller' => 'messages',
'action' => 'messages'
)
);
用法:
echo Router::url(array(
'controller' => 'messages',
'action' => 'messages',
823214
));
// /messages/823214
这有一些缺点,因为用这种方法不可能验证/messages/
之后的内容。为避免这种情况,需要使用路由参数
Router::connect('/messages/:id',
array(
'controller' => 'messages',
'action' => 'messages'
),
array(
'id' => '\d+',
)
);
用法:
echo Router::url(array(
'controller' => 'messages',
'action' => 'messages',
'id' => 823214 // <- different usage
));
// /messages/823214
在config / routes.php中
$routes->connect('/NAME-YOU-WANT/:id',
['controller' => 'CONTROLLER-NAME','action'=>'ACTIOn-NAME'])->setPass(['id'])->setPatterns(['id' => '[0-9]+']
);
您可以使用Cake-PHP的路由功能。看看this page。