CakePHP:使用帖子标题作为视图方法的块

编程入门 行业动态 更新时间:2024-10-26 04:32:30
本文介绍了CakePHP:使用帖子标题作为视图方法的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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来寻址网站上的内容,这样您就不必担心处理标题/块更改。对于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

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

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

更多推荐

CakePHP:使用帖子标题作为视图方法的块

本文发布于:2023-10-12 17:32:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   标题   方法   帖子   CakePHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!