且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Symfony 4设置问题(路由器异常)

更新时间:2022-01-18 21:23:54

自新的Symfony Flex安装过程发布以来,一直存在类似的问题.不确定我对基本的怪癖是否已经找到了一个好的答案.

There have been several questions like this since the new Symfony Flex installation process was released. Not sure that I have seen a good answer yet to what is basically a quirk.

假设您安装了基本的框架和Web服务器,然后检查路由:

Assume you install the basic skeleton and web server and then check the routes:

composer create-project symfony/skeleton skeleton
cd skeleton
composer require server
bin/console debug:router

------ -------- -------- ------ ------ 
Name   Method   Scheme   Host   Path  
------ -------- -------- ------ ------

因此,没有开箱即用定义任何路由.您可以通过查看config/routes.yaml或在空的src/Controller目录中确认这一点.

So no routes are defined out of the box. You can confirm this by looking at config/routes.yaml or in the empty src/Controller directory.

那么,如果启动服务器并导航到浏览器中的/,您会期望什么?找不到/的路线.相反,令人惊讶的是,您实际上得到的是一个欢迎页面.嗯这是从哪里来的?实际的FrameWork捆绑包中什么都没有生成.

So what would you expect if you start the server and navigate to / in your browser? No route found for / of course. Instead, surprisingly, you actually get what looks like a welcome page. Hmmm. Where does that come from? Nothing in the actual FrameWork bundle is generating it.

相反,您需要深入研究http-kernel组件,并查看在未定义路由的情况下尝试匹配路由时会发生什么情况.最终,您进入了:

Instead you need to dig down into the http-kernel component and see what happens when you try to match a route when no routes are defined. Eventually you end up in:

namespace Symfony\Component\HttpKernel\EventListener;
class RouterListener implements EventSubscriberInterface
    try {
        // matching a request is more powerful than matching a URL path + context, so try that first
        if ($this->matcher instanceof RequestMatcherInterface) {
            $parameters = $this->matcher->matchRequest($request);
    } catch (ResourceNotFoundException $e) {
        if ($this->debug && $e instanceof NoConfigurationException) {
            $event->setResponse($this->createWelcomeResponse());

基本上,如果尝试匹配路由并且未配置路由器(换句话说,未定义路由),则会显示欢迎页面.为何Symfony开发人员这样做的确切原因可能是个谜.只是需要接受的东西.

Basically, if you try to match a route and the router is not configured (in other words no routes are defined) then the welcome page is presented. Exactly why the Symfony developers did this is perhaps a bit of a mystery. Just something that needs to be accepted.

现在安装探查器并检查路由.

Now install the profiler and check routes.

composer req profiler
bin/console debug:router
-------------------------- -------- -------- ------ ----------------------------------- 
Name                       Method   Scheme   Host   Path                               
-------------------------- -------- -------- ------ ----------------------------------- 
_twig_error_test           ANY      ANY      ANY    /_error/{code}.
    {_format}           
_wdt                       ANY      ANY      ANY    /_wdt/{token}                      
_profiler_home             ANY      ANY      ANY    /_profiler/                        
...

我们有路线.无需配置任何应用程序或框架,但现在已配置了路由系统.而且,如果我们刷新浏览器,欢迎页面就会被看起来不祥的Route Not Found异常神秘地替换.

We have routes. Nothing app or framework specific but the routing system is now configured. And if we refresh the browser, the welcome page is mysteriously replaced by the rather ominous looking Route Not Found exception.

如果安装其他定义路由的捆绑包(例如安全捆绑包),也会发生同样的事情.当然,symfony/website-skeleton定义了一堆内部路由.但不是GET/.

Same thing happens if you install other bundles that define routes such as the security bundle. And of course the symfony/website-skeleton defines a bunch of internel routes. But not GET /.

TLDR:即使看起来像这样,默认情况下也未定义GET/路由.您需要自己添加它.

TLDR: The GET / route is not defined by default even though it might seem like it. You will need to add it yourself.