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

时间:2018-02-06 12:54:41

标签: php symfony

我试图获得Symfony 4设置。我按照此处列出的教程:https://symfony.com/doc/4.0/setup.html

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

我没有改变任何东西,作曲家似乎抓住了所有必要的依赖。我之前运行PHP进行检查,没有任何重要的缺失:

enter image description here

我真的被困在这里,因为这段代码应该正常工作(它适用于我认识的任何人)并且它来自一个可以检查它的来源。

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)

1 个答案:

答案 0 :(得分:6)

自从新的Symfony Flex安装过程发布以来,出现了类似的问题。不确定我已经看到了一个基本上是怪癖的好答案。

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

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目录中确认这一点。

那么,如果您启动服务器并导航到/在浏览器中,您会期望什么?当然没有找到路线。相反,令人惊讶的是,你实际上得到的东西看起来像一个欢迎页面。嗯。它来自哪里?实际的FrameWork包中没有任何东西可以生成它。

相反,您需要深入了解http内核组件,看看在没有定义路由时尝试匹配路由时会发生什么。最终你最终进入:

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开发人员这样做可能有点神秘。只是需要接受的东西。

现在安装探查器并检查路线。

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异常神秘地取代。

如果您安装了定义安全捆绑等路由的其他捆绑包,则会发生同样的情况。当然,symfony / website-skeleton定义了一堆internel路由。但不是GET /.

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