开发者

How to set Custom URL in CakePhp

开发者 https://www.devze.com 2023-03-24 13:42 出处:网络
I would like to开发者_JS百科 define a url like below: http://localhost/bekzcart/books/list?cid=1&type=grid&page=1

I would like to开发者_JS百科 define a url like below:

http://localhost/bekzcart/books/list?cid=1&type=grid&page=1

How to create a custom route for that type of url, my router code is not work at all

Router::connect('/books/list?cid=:cid&type=:type&page=:page', array('controller' => 'books', 'action' => 'list'));

The parameter cid, type, and page is empty, but when i changed to

Router::connect('/books/list?:cid&:type&:page', array('controller' => 'books', 'action' => 'list'));

it works, the parameter cid, type, and page is now exist.

Note: my cake version is 1.3

Thanks in advance,

Brian


I don't think you need those routes, if you need to get those url parameters, you can just get it from $this->params['url']['param_name']

If you want the parameters to be readily available as ${param_name} in your method, you can tell routes to pass those params by using pass, like so:

http://book.cakephp.org/view/949/Passing-parameters-to-action

// some_controller.php
function view($articleID = null, $slug = null) {
    // some code here...
}

// routes.php
Router::connect(
    // E.g. /blog/3-CakePHP_Rocks
    '/blog/:id-:slug',
    array('controller' => 'blog', 'action' => 'view'),
    array(
        // order matters since this will simply map ":id" to $articleID in your action
        'pass' => array('id', 'slug'),
        'id' => '[0-9]+'
    )
);

So in your case, would be something like: (untested)

Router::connect('/books/list?cid=:cid&type=:type&page=:page', 
   array('controller' => 'books', 'action' => 'list'),
   array(
      'pass' => array('cid', 'type', 'page')
   )
);
0

精彩评论

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