Symfony - How to generate URL without controller or path
If you need to generate URL out of controller, you need to create you own Route with necessary path.
$route = new \Symfony\Component\Routing\Route('/calculator/new');
And add this route to Route Collection:
$routes = new \Symfony\Component\Routing\RouteCollection();
$routes->add('calculator_new', $route);
After that you can generate URL:
$generator = new \Symfony\Component\Routing\Generator\UrlGenerator($routes, new \Symfony\Component\Routing\RequestContext());
$url = $generator->generate('calculator_new');
This returns: "/calculator/new"
If you need to add any query parameters:
$url = $generator->generate('calculator_new', ['width' => 100]);
This will return "/calculator/new?width=100" as the generated URL, including the specified query parameter.
In summary, by creating a route, adding it to a collection, and using a URL generator, you can easily generate URLs and include additional query parameters as needed.
Comments