【问题标题】:Routing to a different URLs based on post type根据帖子类型路由到不同的 URL
【发布时间】:2011-01-29 08:37:54
【问题描述】:

设置路由以使不同类型的帖子具有不同的 URL 的最佳方法是什么?

例如,常规帖子是/posts/slug,而精选帖子是/featured/slug

两者都链接到同一个控制器和动作/posts/view/slug

我尝试了不同的方法来做到这一点,但收效甚微。目前我的链接参数如下所示:

array('controller' => 'posts', 'action' => 'view', 'featured' ,$post['Post']['slug'])

编辑:我可以为每种不同的类型创建一个动作,并使用 setAction 来使用视图动作。虽然有更好的方法吗?

array('controller' => 'posts', 'action' => 'featured', $post['Post']['slug'])

function featured($slug) {
    $this->setAction('view', $slug);
}

【问题讨论】:

    标签: php cakephp routing


    【解决方案1】:

    这可能有效:

    // app/config/routes.php
    Router::connect('/featured/:slug', array(
      'controller' => 'posts',
      'action' => 'view',
      'type' => 'featured'
    ));
    Router::connect('/posts/:slug', array(
      'controller' => 'posts',
      'action' => 'view',
      'type' => 'normal'
    ));
    
    // one of your views
    echo $html->link('Featured post title', array(
      'controller' => 'posts',
      'action' => 'view',
      'type' => 'featured',
      'slug' => $post['Post']['slug']
    ));
    or
    echo $html->link('Normal post title', array(
      'controller' => 'posts',
      'action' => 'view',
      'type' => 'normal',
      'slug' => $post['Post']['slug']
    ));
    

    【讨论】:

      【解决方案2】:

      我认为你也可以通过在 /app/config/routes.php 中添加以下行来做到这一点:

      Router::connect('/:controller/featured/*',array('action' => 'view'));
      

      route setting in cookbook

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-18
        • 2018-09-24
        • 2020-10-24
        • 1970-01-01
        • 2019-07-10
        • 2013-07-23
        • 2020-08-02
        • 2020-06-14
        相关资源
        最近更新 更多