简体   繁体   English

Symfony CMF链路由器:尽管路由匹配,但仍收到404错误

[英]Symfony CMF chain router: getting 404 error despite route matching

I am building an app with Symfony 4 that can be extended by plugins. 我正在用Symfony 4构建一个可以通过插件扩展的应用程序。 Plugins are essentially bundles, but they are just dropped into a folder and don't have to be activated (for example, in config/bundles.php or in config/routes/ ). 插件本质上是捆绑软件,但是它们只是放到一个文件夹中,不必激活(例如,在config/bundles.phpconfig/routes/ )。

In order for plugins to register routes automatically, I have CMF chain router override the default symfony @router service. 为了让插件自动注册路由,我让CMF链路由器覆盖了默认的symfony @router服务。 Each plugin can then have a service tagged router , which the chain router adds to the chain. 然后,每个插件可以具有一个带有服务标签的router ,链路由器将其添加到链中。 This part works. 这部分有效。

In order to make it easier for plugins to register routes, the core app provides an AnnotationRouter class, which takes a path to look for annotated controllers in. The plugins would then register a service like this: 为了使插件更容易注册路由,核心应用提供了AnnotationRouter类,该类采用路径在其中查找带注释的控制器。然后,插件将注册如下服务:

sample_plugin.router:
    class: MyApp\Routing\AnnotationRouter
    arguments: ['@service_container', '@@SamplePlugin/Controller']
    tags:
      - { name: router, priority: 20 }

However, these routes behave very strangely. 但是,这些路线的行为非常奇怪。 In fact, they only work on the first request after clearing the cache! 实际上,它们仅在清除缓存后才对第一个请求起作用! All subsequent requests return 404 errors. 所有后续请求均返回404错误。 This is what the 'Routing' tab in the symfony profiler looks like. 这就是symfony探查器中“路由”选项卡的外观。

symfony探查器在后续请求上的屏幕截图

On the top it says it doesn't match a route, but on the bottom it matches! 顶部显示不匹配路线,但底部显示匹配! I don't know what to make of this. 我不知道该怎么做。

Also, bin/console router:match /test2 matches the route and bin/console debug:router lists the route. 同样, bin/console router:match /test2匹配路由,而bin/console debug:router列出路由。 All the routes of the default symfony router (which is the only other router in the chain besides the plugin's router) work as they should. 默认symfony路由器(除了插件的路由器之外,它是链中唯一的其他路由器)的所有路由都可以正常工作。

Interestingly, all of this has worked before, when still using symfony 3. 有趣的是,所有这些在以前仍在使用symfony 3时已经起作用。

Ok, turns out this is not an issue with CMF Routing chain router after all, but rather with the implementation of the actual router. 好的,事实证明,这毕竟不是CMF路由链路由器的问题,而是实际路由器的实现。

My router implementation is as described in this question . 我的路由器实现如本问题所述 The problem had to do with Doctrine's annotation reader and annotation autoloading. 该问题与Doctrine的注释阅读器和注释自动加载有关。 I ended up installing indigophp/doctrine-annotation-autoload , which apparently is only a workaround though. 我最终安装了indigophp/doctrine-annotation-autoload ,但这显然只是一种解决方法。

See also Autoloading annotation classes on GitHub. 另请参见在GitHub上自动加载注释类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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