简体   繁体   中英

Symfony 2: Find matching firewall, based on route name

My ultimate goal is to check whether a certain route name falls within the secured area of the application.

I am thinking that to achieve that, I should parse security.yml, get the "firewalls" section and go through each firewall trying to match the path of my route name to the pattern of the "secured_area" firewall.

Being new to Symfony2, I tried digging in its source to find how it parses the security.yml itself, but I'm a bit overwhelmed by the number of classes involved in the process.

So, I'm asking for advice:

  1. Is this the correct way to approach the problem or is there a more straight forward solution?

  2. Any hints on how to write this?

Well, I ended up doing something along those lines:

    $route = $this->router->getRouteCollection()->get($routeName);
    $yamlParser = new Yaml\Parser();
    $securityConfig = $yamlParser->parse(file_get_contents($this->securityFilePath));
    foreach ($securityConfig['security']['firewalls'] as $firewallName => $definition) {
        if (isset($definition['pattern']) && preg_match('{'.$definition['pattern'].'}', $route->getPath())) {
            return $firewallName;
        }
    }

May not be the cleanest solution, but does the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM