3

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!

tekniskt
  • 483
  • 3
  • 10
  • Why don't you simply use `mod_rewite` to rewrite the urls before Cake sees them? – Alexandre Danault Jun 14 '13 at 14:58
  • I'd like to keep all the routing within the Cake app if possible. `mod_rewrite` (or in this case the `HttpRewriteModule` for Nginx) are certainly options but it's another place for things to be configured. – tekniskt Jun 14 '13 at 15:08
  • Im not sure if it's possible with just basic routing settings, when you persist that this param is action name, if it would be a slug or any named params that wouldn't a problem. If you do really need to make it that way, then the only way i know you can do it is making changes in cake's dispatched object so it would translate dashes to underscores before calling an action on controller, but that's modifying core - so no updates. Maybe there is some other way someone has found, try looking for some plugins – johhniedoe Aug 22 '13 at 13:33

1 Answers1

0

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?

Community
  • 1
  • 1
MrSynAckSter
  • 1,681
  • 1
  • 18
  • 34