Вопрос

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). Note that the url has dashes and the action has underscores.

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

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.

Thanks!

Это было полезно?

Решение

This is fighting against CakePHP's naming conventions for URLs 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. http://book.cakephp.org/2.0/en/installation/url-rewriting.html may give you some ideas for what you want to do.

I asked a related question some time ago. Is it possible to use Route::Connect to route to a query string parameter?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top