【问题标题】:cakephp url modification: remove action and add slug inflectorcakephp url 修改:删除动作并添加 slug 变形器
【发布时间】:2014-07-28 07:22:39
【问题描述】:

我正在尝试删除 cakephp url 中的操作并添加一个 slug 变形器,更清楚地说这是我的预期输出:
从这个:example.com/posts/view/81/这是一个测试帖子
到这个:example.com/posts/This-is-a-test-post

这是我当前的代码:
这给了我这个输出:example.com/posts/view/This is a test post

控制器:

public function view($title = null) {
        if (!$title) {
            throw new NotFoundException(__('Invalid post'));
        }
        $post = $this->Post->findByTitle($title);
        if (!$post) {
            throw new NotFoundException(__('Invalid post'));
        }
        $this->set('post', $post);
    }

view.ctp:

$this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['title']));

我也试过这个,这个链接是我的参考CakePHP: Use post title as the slug for view method
输出:example.com/posts/view/21/This-is-a-test-post

控制器:

function view($id) {
        $this->Post->id = $id;
        $this->set('post', $this->Post->read());
    }

view.ctp

$this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['id'], Inflector::slug($post['Post']['title'],'-')));

以下是我尝试但失败的其他链接,我也尝试修改 routes.php 但无法制作正确的代码使其工作

want to remove action name from url CakePHP
How to remove action name from url in cakephp?
Remove action name from Url in cakephp?

感谢任何帮助/建议。

【问题讨论】:

    标签: url cakephp controller routes action


    【解决方案1】:

    使用 Id 和命名参数...

    在 routes.php 上

    将新路由定义为-

    Router::connect('/posts/:id-:title',
        array('controller' => 'posts',
            'action' => 'view')
    );
    

    这个新定义的路由将匹配并解析所有包含 id 和 title 命名参数的 url..

    重要提示: 不要在 routes.php 结束后定义新的路由。尝试在文件中间定义..

    正在查看

    echo $this->Html->link('Hi',array(
        'controller' => 'posts',
        'action' => 'view',
        'id' => 4,
        'title' => Inflector::slug('the quick brown fox')
    ));
    

    【讨论】:

      【解决方案2】:

      上面提供的解决方案将满足您的需求,但在阅读您的问题后,我仍然想到一件事......在您的控制器中,您正在尝试使用标题阅读帖子我的意思是这会减慢您的系统,不推荐编程所以使用 id 并在您的数据库表中增加一列用于 slug(SEO 标题),这将用于创建您的帖子网址。

      For example: 
      Post title: This is a test post 
      Create seo title for this as: this-is-a-test-post-123
      Stor seo title in DB as <this-is-a-test-post>
      

      看到 123 是您的帖子 ID 现在更改您的控制器功能以根据 id ie.123 获取数据,我希望您可以轻松地从 sting 中提取 123...

      注意:记住你必须考虑this-is-a-123字符串也是因为 他们还登陆了 id 为 123 的帖子。

      上述解决方案使用以下路线:

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

      现在在您的控制器中:

      您将在 $post_title 中获得字符串“this-is-a-test-post-123”

      function view($post_title=null){   
      
                 $temp = explode('-',$post_title);
                 $posts_id= end($temp); 
                 $lastKey = end(array_keys($temp));
                 unset($temp[$lastKey]);
                 $seoTitle = implode("-",$temp);
                 //Now compare the above seoTitle with DB seo title for unique urls
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2012-12-11
        • 2014-06-13
        • 2011-09-07
        • 2013-04-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-04
        • 2018-03-20
        • 1970-01-01
        相关资源
        最近更新 更多