开发者

CakePHP Routing assistance

开发者 https://www.devze.com 2023-01-19 10:26 出处:网络
I have a users module and few actions like wall, dishes, restaurants, photos. I wanna set up a routing like something like this

I have a users module and few actions like wall, dishes, restaurants, photos.

I wanna set up a routing like something like this

site.com/users/**{userId or Username}**/wall  => *should route to wall() 开发者_C百科action*

site.com/users/**{userId or Username}**/dishes => *should route to dishes() action*

site.com/users/**{userId or Username}**/restaurants => *should route to restaurants() action*

site.com/users/**{userId or Username}**/photos => *should route to photos() action*

I am not sure how to keep the url intact in the address bar but load the actions.. where user id or username is passed onto the action.


Try:

Router::connect(
    '/users/:id/:action',
    array(
      'controller' => 'users'
      'id' => '[0-9]+') # or [a-zA-Z0-9] for username
);

Also in those action (wall, dishes...) you need to add:

$id = $this->params['id'];


http://bakery.cakephp.org/articles/view/cakephp-s-routing-explained

<?php
Router::connect(
    '/writings/:username/:action/:id/*', 
    array(
        'controller' => 'articles'
    ),
    array(
        'pass' => array(
            'id',
            'username'
        )
    )
);
?> 

Having this route makes CakePHP call your action like $Controller->show(69, 'phally') and then your action should look like:

<?php
public function show($id = null, $username = null) {
    // $id == 69;
    // $username == 'phally';
}
?> 
0

精彩评论

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