使用Cake PHP路由器自动从破折号转换为下划线

时间:2013-06-14 14:47:36

标签: php cakephp cakephp-routing

我一直在设置这样的路线:

Router::connect('/background/a-page', array('controller' => 'background', 'action' => 'a_page'));
Router::connect('/background/another-page', array('controller' => 'background', 'action' => 'another_page'));
Router::connect('/background/my-third-page', array('controller' => 'background', 'action' => 'my_third_page'));
// More routes here

我想用这样的路线替换它们:

Router::connect('/background/:action', array('controller' => 'background'));

其中网址/background/my-third-page将映射到操作my_third_page(在后台控制器中)。请注意,url有破折号,动作有下划线。

目前Cake无法将转换从破折号映射到下划线,因此我的新路线失败了:/background/my-third-page但这样做有效:/background/my_third_page

我想在网址中保留破折号。有没有办法让Cake将破折号映射到下划线?

我也喜欢反向路由从下划线映射到破折号,所以:

$this->Html->link('View',
    array('controller' => 'background', 'action' => 'my_third_page')
);

将映射到:background/my-third-page

谢谢!

1 个答案:

答案 0 :(得分:0)

这是针对CakePHP的URL http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html的命名约定。你最好使用mod重写这种东西 - 再次,因为这与通常在蛋糕中完成URL的方式相反,蛋糕会对你起作用。 http://book.cakephp.org/2.0/en/installation/url-rewriting.html可能会为您提供一些想法。

前一段时间我问了一个相关的问题。 Is it possible to use Route::Connect to route to a query string parameter?