开发者

CakePHP advanced routing

How do I create URLs like this (English as default):

For action index in Categories controller:

  • /categories
  • /en/categories
  • /de/categories
  • ...

for action view in Categories controller:


What's missing from the article is the useful persist feature in available in the Router::connect().
Which will add the language parameter to all generated urls.

Example route:

Router::connect(
  '/:language/:controller',
  array(),
  array('language' => '[a-z]{2}', 'persist' => array('language'))
);
Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{2}', 'persist' => array('language')));


This is exactly what custom routes in CakePHP are made for. You can define your own router class that handles all that logic for you and mitigates everything before running through the dispatcher. I recently did exactly what you're looking to do by referencing this blog post from Mark Story himself: http://mark-story.com/posts/view/using-custom-route-classes-in-cakephp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜