Background: I want to alter a self written Twig extension. The class is defined as follows:
class pagination extends \Twig_Extension {
protected $generator;
public function __construct($generator){
$this->generator = $generator;
}
....
}
In one of the methods i want to generate an URL like this:
$this->generator->generate($route, array('routeParam' => $value);
But the problem is, that some routes don't have the param 'routeParam' whichg would cause an Exception when generating the route this way.
My question is: How can i find out if a given route has certain paramters in that method?
The first way is through the route snapshot. The route snapshot provides the initial value of the route parameter map (called the paramMap ). You can access the parameters directly without subscribing or adding observable operators. The paramMap provides methods to handle parameter access like get , getAll , and has .
Laravel routes are located in the app/Http/routes. php file. A route usually has the URL path, and a handler function callback, which is usually a function written in a certain controller.
Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys.
The reason that the params property on ActivatedRoute is an Observable is that the router may not recreate the component when navigating to the same component. In this case the parameter may change without the component being recreated.
To check your route has all the params you need to compile your route , to compile route you need router service so pass @service_container
service to your twig extension by adding in your service definition
somename.twig.pagination_extension:
class: Yournamesapce\YourBundle\Twig\Pagination
arguments: [ '@your_generator_service','@service_container' ]
tags:
- { name: twig.extension } ...
And in your class get container then get router service from container and get all routes by getRouteCollection()
once you have all routes get the desired route fro collection by $routes->get($route)
then compile that route,once your have a complied route definition you can get all the params required for route by calling getVariables()
which will return array of parameters and before generating check in array if routeParam
exists
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
class Pagination extends \Twig_Extension {
protected $generator;
private $container;
public function __construct($generator,Container $container){
$this->generator = $generator;
$this->container = $container;
}
public function somefunction(){
$routes = $this->container->get('router')->getRouteCollection();
$routeDefinition = $routes->get($route);
$compiledRoute = $routeDefinition->compile();
$all_params = $compiledRoute->getVariables();
if(in_array('routeParam',$all_params)){
$this->generator->generate($route, array('routeParam' => $value);
}
}
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With