我一直在尝试使用以下方法来创建如下网址:
I have been trying to do the following method to create urls like:
domain/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 来寻址您网站上的内容,这样您就不必担心处理标题/slug 更改.从 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/posts/13/This_is_a_test_post请注意,slug 没有做任何事情,而是为您提供了 SEO 的好处
Note that the slug isn't doing anything, but is giving you the benefit of SEO
更多推荐
CakePHP:使用帖子标题作为视图方法的 slug
发布评论