I need to have dynamic routes with symfony 2 where a slug parameter is added to the url and is related to a page in the database. Each page has a own slug and its content stored in the database. I read the article Advanced Routing but it’s for the old version of symfony. For the new version it seems that ParamConverter does a similar job. Is this the correct way to implement a doctrine-based routing or should I write a real custom router class?
Share
I would use a ParamConverter, yes. The default DoctrineParamConverter that ships with the FrameworkExtraBundle can handle most simple cases — that is, it knows how to look up a typehinted object by a field with the same name as the route placeholder:
Normally, in a controller’s argument list, you’d have a
$slugvariable that would be populated from the contents of{slug}captured by the route. However, with the ParamConverter, it recognizes that you’re requesting aFooEntityclass, and will try to find that entity by the capturedslugvalue and populate the$foovariable with that entity.The default ParamConverter is, of course, limited to only being able to look up properties that actually exist on the entity: if FooEntity does not have a field named
slug, the lookup will fail and an exception will be thrown. Like I said, this will handle a majority of basic use cases. If you need more in-depth conversion of request parameters, you could always write your own.