开发者

Cakephp routing with optional params

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/catego开发者_高级运维ry:dog

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜