我的作品集有以下链接结构:
<?php echo $this->Html->link($post['Portfolio']['title'], array('controller' => 'portfolio', 'action' => 'view', Inflector::slug($post['Portfolio']['title'])), array('title' => $post['Portfolio']['title'])); ?>它给出的urls如下:http://driz.co.uk/portfolio/view/Paperview_Magazine
但是,如何让我的控制器根据标题显示项目呢?
到目前为止,我已经有了这个,但还没能让它工作,只能得到一个空白页面(所以我还需要检查格式是否正确,以及它们是否是相关的项目)
function view ( $title )
{
$posts = $this->Portfolio->find('first', array('conditions' => array('Portfolio.title' => $title)
));
if (empty($title))
{
$this->cakeError('error404');
}
$this->set(compact('posts'));
}发布于 2011-06-13 03:35:14
@Ross建议你使用Portfolio.slug进行搜索,所以你可以这样做:
并将其保存到数据库中。在保存数据之前,您始终可以在模型的beforeSave事件中执行此操作,或者在控制器中执行此操作。
不幸的是,没有办法反转Inflector::Slug函数,因为它删除了某些字符,如撇号、引号、圆括号等,这就是为什么如果您想要搜索它,需要将slug保存到数据库中。
下面是如何在模型中使用beforeSave事件:
public function beforeSave(array $options = array())
{
// If the title is not empty, create/update the slug.
if ( ! empty($this->data[$this->alias]['title'] )
$this->data[$this->alias]['slug'] = Inflector::slug($this->data[$this->alias]['title']);
// Returning true is important otherwise the save or saveAll call will fail.
return true;
}https://stackoverflow.com/questions/6323930
复制相似问题