简体   繁体   中英

How to find the current route pattern in Symfony 3.4?

像在security.yml中一样- { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY }我想在路由模式比/admin延迟的情况下在eventListener中执行代码

If you are listening to a Symfony Kernel-event you will be able to retrieve the current request from the event by calling getRequest() on the event. You might have to check if it's the current request, ie you are not dealing with a sub-request, but I will omit this here.

Once you have the request checking if a route matches a pattern or is not captured by the pattern is as easy as doing a preg_match() or in simpler cases just a string comparison like this:

if (strpos($request->getPathInfo(), '/admin/') === 0) {
    return
}

// Do your thing

This will skip the event when your route starts with /admin/ and execute whatever you put instead of the comment if it does not match this admin pattern.

If you want to check if a certain route name has matched you can also fetch the _route attribute from the request:

$routeName = $request->attributes->get('_route');

You can then check the router for additional info on that route, deal with route names, eg by checking a certain prefix and do whatever else you want.

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