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:
- /categories/englishslug
- /de/categor开发者_如何学JAVAies/deutshslug
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
精彩评论