简体   繁体   English

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

[英]Automatically convert from dashes to underscores using the Cake PHP router

I've been setting up some routes like this: 我一直在设置这样的路线:

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

I would like to replace them with a route like this: 我想用这样的路线替换它们:

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

where the url /background/my-third-page would map to the action my_third_page (in the background controller). url /background/my-third-page将映射到动作my_third_page (在后台控制器中)。 Note that the url has dashes and the action has underscores. 请注意,url有破折号,动作有下划线。

Currently Cake fails to map the conversion from dashes to underscores, so with my new route this fails: /background/my-third-page but this works: /background/my_third_page 目前Cake无法将转换从破折号映射到下划线,因此我的新路线失败了: /background/my-third-page但这样/background/my_third_page/background/my_third_page

I want to keep dashes in the urls. 我想在网址中保留破折号。 Is there any way to make Cake map the dashes to underscores? 有没有办法让蛋糕将破折号映射到下划线?

I'd also like the reverse routing to map from underscores to dashes, so: 我也喜欢反向路由从下划线映射到破折号,所以:

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

would map to: background/my-third-page . 将映射到: background/my-third-page

Thanks! 谢谢!

This is fighting against CakePHP's naming conventions for URLs http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html . 这是针对CakePHP的URL命名约定http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html You are better off using mod rewrite for this sort of thing - and again, since this is against the way URLs are normally done in cake, cake will fight you tooth and nail. 你最好使用mod重写这种东西 - 再次,因为这与通常在蛋糕中完成URL的方式相反,蛋糕会对你起作用。 http://book.cakephp.org/2.0/en/installation/url-rewriting.html may give you some ideas for what you want to do. http://book.cakephp.org/2.0/en/installation/url-rewriting.html可能会为您提供有关您想要做的事情的一些想法。

I asked a related question some time ago. 我前一段时间问了一个相关的问题。 Is it possible to use Route::Connect to route to a query string parameter? 是否可以使用Route :: Connect路由到查询字符串参数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM