user3054209
user3054209

Reputation: 13

CakePHP routes always pointing to same action

I am facing some problems in routing under cakephp

there are three actions in my controller

they are as below

www.example.com/photos/newphotos
www.example.com/photos/random
www.example.com/photos/popular

I want them as

www.example.com/newphotos
www.example.com/random
www.example.com/popular

so i routes file under config file I wrote as

Router::connect('/:newphotos', array('controller' => 'photos', 'action' => 'newphotos'));
Router::connect('/:popular', array('controller' => 'photos', 'action' => 'popular'));
Router::connect('/:random', array('controller' => 'photos', 'action' => 'random'));

its working fine when I hit the url www.example.com/newphotos

but when I hit url www.example.com/random or www.example.com/popular , its again point to action newphotos.

so how can I solve it

(In other words I need to remove controller name "photos" from url for every action)

Many thanks

Upvotes: 0

Views: 111

Answers (2)

linkyndy
linkyndy

Reputation: 17928

Why not remove the : from the routes?

If you want to stick with /: paths, then you would need to supply a third parameter to Router::connect() in which to specify patterns for the added options. That is, if you have /:popular as the first parameter, you would need array('popular' => 'popular') as the third parameter, making the rule look like:

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

This means that :popular will be matched against the given regex, that is the literal 'popular'. See CakePHP's docs for more info.

Nevertheless, this is useless and silly, so you should stick with paths without colons.

Upvotes: 1

Roberto Maldonado
Roberto Maldonado

Reputation: 1595

Just delete the colon from the first parameter. They are kind of "capturing variables", so now you basically are routing all / with some parameters to photos/newphotos, and the parameters being captured to :newphotos. As it always will match the first route, then it will not look for the others.

Upvotes: 0

Related Questions