-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Autowire route parameters with the PropertyAccess component #20083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is probably related with this old pending issue: #10395 |
@bocharsky-bw my bundle provides this functionality in a similar fashion: https://github.com/iltar/http-bundle It doesn't allow a direct value of your object (yet). I have not found out a proper way to do this. If you do know, feel free to open a PR. |
It's similar to the #10395 but idea is a bit different and more simple as for me. I suggest to make it possible passing whole object to the abstract class Controller implements ContainerAwareInterface
{
protected function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
{
if (is_object($parameters)) {
$object = $parameters;
$parameters = [];
$propertyAccessor = $this->get('property_accessor');
$variables = $this->get('router')->getRouteCollection()->get($route)->compile()->getVariables();
foreach ($variables as $propertyName) {
if ($propertyAccessor->isReadable($object, $propertyName)) {
$parameters[$propertyName] = $propertyAccessor->getValue($object, $propertyName);
}
}
}
return $this->container->get('router')->generate($route, $parameters, $referenceType);
}
} Of course, this feature does not cover all cases , but some simple like in my example to this issue it handles well. I think in a lot of projects many routes has similar construction: placeholders in routes matches with property names on an entity. |
Your case only covers the Controller class like this, which is nice, but the idea is that the controller class merely provides a shortcut for several frequently used services/methods, not to actually provide a unique feature set. I don't know if the See:
While this feature is really nice, I don't think the overhead is worth it in the core as resolving the arguments simply costs more time, even though this is a great DX part. |
Now I should pass route parameters manually when generating URL to a route in any controller which extends
Symfony\Bundle\FrameworkBundle\Controller
:What about some sort of autowiring. i.e. allow populating route parameters automatically from specified object:
If an object was passed to the
generateUrl()
method instead of an array as the 2nd argument - let's use the PropertyAccess component to populate route parameters automatically from passed object.BTW, we can't do it without BC break for the Router component, but can do for
path()
/url()
Twig functions andgenerateUrl()
method ofSymfony\Bundle\FrameworkBundle\Controller
as it does not implementRouterInterface
.The text was updated successfully, but these errors were encountered: