开发者

Retaining parent slugs in CakePHP

开发者 https://www.devze.com 2023-04-09 21:20 出处:网络
I\'m experimenting with SEO friendly URL\'s in CakePHP as efficiently as I can, I\'ve managed to use the current format, each example uses function view($slug) except for the first example which uses

I'm experimenting with SEO friendly URL's in CakePHP as efficiently as I can, I've managed to use the current format, each example uses function view($slug) except for the first example which uses function index().

/categories/

/categories/books/

/categories/books/it-and-comput开发者_JAVA技巧ing/

But what if IT & Computing has a sub-category "Web Development"? I'd like the URL to become:

/categories/books/it-and-computing/web-development/

I'm not sure how to do this without creating too many routes. Here is my route code so far:

Router::connect('/categories/', array('controller' => 'categories', 'action' => 'index'));

Router::connect('/categories/:slug', array('controller' => 'categories', 'action' => 'view'), array('pass' => array('slug')) );

Router::connect('/categories/:parent/:slug', array('controller' => 'categories', 'action' => 'view'), array('pass' => array('parent', 'slug')) );

Any help would be greatly appreciated

Kind Regards

Stephen


// in routes.php
Router::connect('/categories/:row:lastslash',array('controller' => 'settings', 'action' => 'show',),array(
      'pass'=>array('row'),
      'row'=>'.*?',
      'lastslash'=>'\/?'
));

//in controller
function show($row = ""){
  if($row){
    $categories = split('/',$row);

    ?><pre><? print_r($categories);?></pre><?die();
  }else{
    die('do something else');
  }
}

/categories/books/computing/web-development/cakephp/

result:

Array
(
    [0] => books
    [1] => computing
    [2] => web-development
    [3] => cakephp
)

/categories/

result:

do something else

/categories/books

result:

Array
(
    [0] => books
)
0

精彩评论

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