views:

140

answers:

2

I have a method in my users controller similar to:

function members($type = null, $category = null) { ... }

Both params are optional and can be used together or on their own.

So with the following route.

Router::connect('/members/*', array('controller' => 'users', 'action' => 'members'));

http://example.com/users/members successfully becomes http://example.com/members.

Unfortunately the following don't work

http://example.com/members/type:cat
http://example.com/members/category:dog
http://example.com/members/type:cat/category:dog

how could I set up my routes so that they all work properly?

A: 

Try with

Router::connect('/members/type\:(.*)', array('controller' => 'users', 'action' => 'members_type'));
Router::connect('/members/category\:(.*)', array('controller' => 'users', 'action' => 'members_category'));
Router::connect('/members/type\:(.*)/category:(.*)', array('controller' => 'users', 'action' => 'members_type'));

Note that I didn't test it, but I think you must escape the colon.

metrobalderas
+2  A: 

Named parameters aren't automagicaly mapped to the action. You can either get them by calling

$this->passedArgs['type'] or $this->passedArgs['category']

or by using the 3rd parameter in the Router::connect:

Router::connect(
    '/members/*',
    array('controller' => 'users', 'action' => 'members'),
    array(
        'pass' => array('type', 'category')
    )
);

http://book.cakephp.org/view/46/Routes-Configuration

harpax
Strangely enough this does work when typing in the url directly although reverse routing does not. $html->link('my link', array('controller' => users, 'action' => 'members', 'type' => 'hello', 'category' => 'world')); results in the regular style url. Is there something else you have to do before reverse routing works?
DanCake
are there other routes set up? If so, there is a chance that another connect call takes care of the reverse routing..
harpax
Router::connectNamed(array('type', 'category')); seems to work.
DanCake