【发布时间】:2014-07-28 07:22:39
【问题描述】:
我正在尝试删除 cakephp url 中的操作并添加一个 slug 变形器,更清楚地说这是我的预期输出:
从这个:example.com/posts/view/81/这是一个测试帖子
到这个:example.com/posts/This-is-a-test-post
这是我当前的代码:
这给了我这个输出:example.com/posts/view/This is a test post
控制器:
public function view($title = null) {
if (!$title) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findByTitle($title);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
view.ctp:
$this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['title']));
我也试过这个,这个链接是我的参考CakePHP: Use post title as the slug for view method:
输出:example.com/posts/view/21/This-is-a-test-post
控制器:
function view($id) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
view.ctp
$this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['id'], Inflector::slug($post['Post']['title'],'-')));
以下是我尝试但失败的其他链接,我也尝试修改 routes.php 但无法制作正确的代码使其工作
want to remove action name from url CakePHP
How to remove action name from url in cakephp?
Remove action name from Url in cakephp?
感谢任何帮助/建议。
【问题讨论】:
标签: url cakephp controller routes action