在 cakephp 3.x 中使用 URL slug
Use URL slug in cakephp 3.x
我可以毫无问题地在 cakephp 3.x 中创建带有标题的记录 slug。现在我想在 URL 上使用那个字段 slug。
我该怎么做?
我尝试使用典型的函数视图将 $id 更改为 $slug...
public function view($slug = null)
{
$noticia = $this->Noticias->get($slug, [
'contain' => ['Categorias', 'Usuarios', 'Etiquetas', 'Fotos']
]);
$this->set('noticia', $noticia);
$this->set('_serialize', ['noticia']);
}
但我有以下错误:"Record not found in table "noticias".
谢谢
get()
方法只能用于通过主键查找记录。相反,您需要使用 find()
:
$noticia = $this->Noticias
->findBySlug($slug)
->contain(['Categorias', 'Usuarios', 'Etiquetas', 'Fotos'])
->firstOrFail();
我可以毫无问题地在 cakephp 3.x 中创建带有标题的记录 slug。现在我想在 URL 上使用那个字段 slug。
我该怎么做?
我尝试使用典型的函数视图将 $id 更改为 $slug...
public function view($slug = null)
{
$noticia = $this->Noticias->get($slug, [
'contain' => ['Categorias', 'Usuarios', 'Etiquetas', 'Fotos']
]);
$this->set('noticia', $noticia);
$this->set('_serialize', ['noticia']);
}
但我有以下错误:"Record not found in table "noticias".
谢谢
get()
方法只能用于通过主键查找记录。相反,您需要使用 find()
:
$noticia = $this->Noticias
->findBySlug($slug)
->contain(['Categorias', 'Usuarios', 'Etiquetas', 'Fotos'])
->firstOrFail();