CakePHP:使用帖子标题作为视图方法的块 [英] CakePHP: Use post title as the slug for view method

查看:91
本文介绍了CakePHP:使用帖子标题作为视图方法的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用以下方法来创建网址:

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

domain.com/portfolio/This_is_a_test_post

function view ( $title )
{       
    $post = $this->Portfolio->find('first', array('conditions' => array(Inflector::slug('Portfolio.title') => $title)));

    //$posts = $this->Portfolio->title = Inflector::slug($title);

    if (empty($title))
    {
        $this->cakeError('error404');
    }
    else
    {
        $this->set(compact('post'));
    }
}

但它不显示帖子!显然,我做错了什么...任何想法如何我可以解决这个问题?感谢

However it doesn't show the post! Obviously I'm doing something wrong... Any ideas on how I could fix this? Thanks

推荐答案

我建议您使用ID来寻址网站上的内容,这样您就不必担心处理标题/块更改。对于SEO透视图你可以很容易地使用Slug,而无需在技术上做任何事情:

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

注lug子没有做任何事情,但是给你SEO

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

这篇关于CakePHP:使用帖子标题作为视图方法的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆