如何在php中找出路由的参数

时间:2022-04-07 09:54:19

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:

在其中一个方法中,我想生成这样的URL:

$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.

但问题是,有些航线没有param 'routeParam',当以这种方式生成航线时,这会导致异常。

My question is: How can i find out if a given route has certain paramters in that method?

我的问题是:我怎样才能知道某条路线是否有特定的参数?

1 个解决方案

#1


6  

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

要检查您的路由,您需要编译您的路由的所有参数,要编译路由您需要路由器服务,所以通过添加您的服务定义将@service_container服务传递给您的twig扩展。

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

你们班上,把路由器服务容器和容器然后被getRouteCollection所有航线()一旦你所有路线被美元所需的路线来收集路线- >得到(美元),那么编译这条路线,一旦你拥有了一个执行路由定义可以得到所需的所有参数的路线通过调用getVariables()将返回数组的参数和生成之前检查数组如果routeParam存在

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);
        }
    }
    ....
}

#1


6  

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

要检查您的路由,您需要编译您的路由的所有参数,要编译路由您需要路由器服务,所以通过添加您的服务定义将@service_container服务传递给您的twig扩展。

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

你们班上,把路由器服务容器和容器然后被getRouteCollection所有航线()一旦你所有路线被美元所需的路线来收集路线- >得到(美元),那么编译这条路线,一旦你拥有了一个执行路由定义可以得到所需的所有参数的路线通过调用getVariables()将返回数组的参数和生成之前检查数组如果routeParam存在

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);
        }
    }
    ....
}