CakePHP: Use post title as the slug for view method

后端 未结 4 707
情深已故
情深已故 2020-12-22 06:44

I have been trying to do the following method to create urls like:

domain.com/portfolio/This_is_a_test_post

function view ( $title )
{           


        
4条回答
  •  清歌不尽
    2020-12-22 07:45

    I would recommend using the ID for addressing content on your website, this way you do not have to worry about dealing with title/slug changes. For a SEO perspective you can easily use the Slug without doing anything with it technically:

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

    And in your view, create links like this:

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

    Now your URL's will look like this:

    domain.com/posts/13/This_is_a_test_post
    

    Note that the slug isn't doing anything, but is giving you the benefit of SEO

提交回复
热议问题