在 CakePHP 中保留父级 slugs

发布于 2024-12-07 20:40:24 字数 875 浏览 2 评论 0原文

我正在尽可能高效地在 CakePHP 中尝试 SEO 友好的 URL,我已经设法使用当前格式,每个示例都使用函数 view($slug),除了第一个示例使用函数 index()。

/类别/

/类别/书籍/

/categories/books/it-and-computing/

但是如果 IT 与计算又会怎样呢?计算有一个子类别“Web 开发”?我希望网址变成:

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

我不知道如何在不创建太多路线的情况下做到这一点。这是到目前为止我的路线代码:

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

路由器::connect('/categories/:slug', array('控制器' => '类别', '操作' => '视图'), 数组('pass' => 数组('slug')) );

路由器::connect('/categories/:parent/:slug', array('控制器' => '类别', '操作' => '视图'), array('pass' => array('parent', 'slug')) );

任何帮助将不胜感激

亲切的问候

斯蒂芬

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-computing/

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几味少女 2024-12-14 20:40:24
// 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/

结果:

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

/categories/

结果:

do something else

/categories/books

结果:

Array
(
    [0] => books
)
// 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
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文