Mar 9, 2009
How can i build dynamic urls with Symfony?
I have a simple article table with a id and title column.
I won’t to have a url like this.
/my-title-4 /:title-:id
Also if the title was changed it must redirect to the actual url.
Solution
add a new route to your routing.yml:
article_detail:
url: /:slug
class: sfDoctrineRoute
options: { model: Article, type: object, method: getBySlug }
param: { module: offer, action: show }
requirements:
slug: .+-\d+
sf_method: [get]define getBySlug function in lib/model/doctrine/ArticleTable.class.php:
1 2 3 4 5 6 7 8 9 10 | public function getBySlug(array $parameters) { if(!preg_match('/\d+$/', $parameters['slug'], $matches)) { return null; } list($id) = $matches; return $this->find($id); } |
and this code to your show action:
1 2 3 4 5 6 7 8 9 10 | public function executeShow(sfWebRequest $request) { $this->article = $this->getRoute()->getObject(); $this->forward404Unless($this->article); if($request->getParameter('slug') != $this->article->getslug()) { $this->redirect($this->generateUrl('article_detail', $this->article)); } } |
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.











Recent Comments