Symfony1 symfony:permalinks(在URL中隐藏id)
我已经阅读了关于永久链接(隐藏主键并用有效字符串替换它们)的段落,但我不理解它是如何工作的这段代码:Symfony1 symfony:permalinks(在URL中隐藏id),symfony1,doctrine,permalinks,Symfony1,Doctrine,Permalinks,我已经阅读了关于永久链接(隐藏主键并用有效字符串替换它们)的段落,但我不理解它是如何工作的这段代码: public function executePermalink($request) { $article = ArticlePeer::retrieveBySlug($request->getParameter('slug'); $this->forward404Unless($article); // Display 404 if no article ma
public function executePermalink($request)
{
$article = ArticlePeer::retrieveBySlug($request->getParameter('slug');
$this->forward404Unless($article); // Display 404 if no article matches slug
$this->article = $article; // Pass the object to the template
}
这个代码是典型的推进代码,对吗?对教义来说有类似的东西吗?我必须编写retrieveBySlug()函数吗?你有没有一个我能理解如何写的例子
非常感谢在Doctrine中,您可以使用一个名为“Sluggable”的扩展 要使其正常工作,您必须更改schema.yml并添加“slaggable”扩展名:
# config/doctrine/schema.yml
Article:
actAs:
Timestampable: ~
Sluggable:
fields: [name]
columns:
name:
type: string(255)
notnull: true
在routing.yml中设置DoctrineRoute
# apps/frontend/config/routing.yml
category:
url: /article/:slug
class: sfDoctrineRoute
param: { module: article, action: show }
options: { model: Article, type: object }
然后在操作代码中,您可以执行以下操作:
public function executeShow(sfWebRequest $request)
{
$this->article = $this->getRoute()->getObject();
$this->forward404Unless($article); // Display 404 if no article matches slug
$this->article = $article; // Pass the object to the template
}
别忘了运行一个原则:在更改模式后构建以重新创建数据库。现在可以正常工作了
# apps/frontend/config/routing.yml
opera_slug:
url: /:sf_culture/opere/:operaslug.html
class: sfDoctrineRoute
param: { module: opera, action: permalink }
options: { model: Opera, type: object }
requirements:
sf_culture: (?:it|en|es|fr)
public function executePermalink(sfWebRequest $request)
{
$this->opera = $this->getRoute()->getObject();
$this->forward404Unless($this->opera); // Display 404 if no article matches slug
//$this->opera = $opera; // Pass the object to the template
}
如您所见,我修改了executePermalink()的最后两行,因为我在使用您的函数时出错@ilanco您可以使用
forward404删除检查,除非因为$this->getRoute()->getObject()
已经执行了检查。您好,我已经尝试过了。。。它给了我一些问题,但我已经解决了(在回答中我解释了我是如何做到的…)很好,但是你可以用:sf\u格式替换.html,并添加到requiremet sf\u格式:html