Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top