Symfony 4设置问题(路由器异常) [英] Symfony 4 setup issues (router exception)

查看:132
本文介绍了Symfony 4设置问题(路由器异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过安装Symfony 4.我按照此处列出的教程进行操作: https://symfony.com/doc/4.0/setup.html

I have tried to get the Symfony 4 setup. I followed the tutorial listed here: https://symfony.com/doc/4.0/setup.html

但是,当我尝试运行服务器时,我得到:

However when I attempt to run the server I get :

我什么都没做,作曲家似乎正在抓住所有必要的依赖关系.我运行PHP进行了更早的检查,没有发现任何严重的缺失:

I haven't changed anything and composer appears to be grabbing all the necessary dependencies. I ran the PHP to check earlier and there was nothing critical missing:

我真的被困在这里,因为此代码应该可以正常工作(它对我认识的其他任何人都有效),并且它是由检查它的来源提供的.

I'm really stuck here as this code should be working (it is working for anyone else I know) and it is from a source who would have checked it.

Update: Result from console from running router debug: 
 -------------------------- -------- -------- ------ -----------------------------------
  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/
  _profiler_search           ANY      ANY      ANY    /_profiler/search
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results
  _profiler_open_file        ANY      ANY      ANY    /_profiler/open
  _profiler                  ANY      ANY      ANY    /_profiler/{token}
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css
 -------------------------- -------- -------- ------ -----------------------------------

堆栈跟踪:

Symfony\Component\Routing\Exception\ResourceNotFoundException:

  at var/cache/dev/srcDevDebugProjectContainerUrlMatcher.php:107
  at srcDevDebugProjectContainerUrlMatcher->match('/')
     (vendor/symfony/routing/Matcher/UrlMatcher.php:95)
  at Symfony\Component\Routing\Matcher\UrlMatcher->matchRequest(object(Request))
     (vendor/symfony/routing/Router.php:262)
  at Symfony\Component\Routing\Router->matchRequest(object(Request))
     (vendor/symfony/http-kernel/EventListener/RouterListener.php:114)
  at Symfony\Component\HttpKernel\EventListener\RouterListener->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher))
  at call_user_func(array(object(RouterListener), 'onKernelRequest'), object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher))
     (vendor/symfony/event-dispatcher/Debug/WrappedListener.php:104)
  at Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(object(GetResponseEvent), 'kernel.request', object(EventDispatcher))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:212)
  at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.request', object(GetResponseEvent))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:44)
  at Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:139)
  at Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor/symfony/http-kernel/HttpKernel.php:125)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:66)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:190)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:37)


    Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
No route found for "GET /" (from "http://localhost/my-project/")

  at vendor/symfony/http-kernel/EventListener/RouterListener.php:144
  at Symfony\Component\HttpKernel\EventListener\RouterListener->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher))
  at call_user_func(array(object(RouterListener), 'onKernelRequest'), object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher))
     (vendor/symfony/event-dispatcher/Debug/WrappedListener.php:104)
  at Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(object(GetResponseEvent), 'kernel.request', object(EventDispatcher))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:212)
  at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.request', object(GetResponseEvent))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:44)
  at Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:139)
  at Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor/symfony/http-kernel/HttpKernel.php:125)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:66)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:190)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:37)

推荐答案

自新的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.

这篇关于Symfony 4设置问题(路由器异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆