Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

php - Symfony 4 setup issues (router exception)

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 : enter image description here

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:

enter image description here

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
 -------------------------- -------- -------- ------ -----------------------------------

Stack traces:

SymfonyComponentRoutingExceptionResourceNotFoundException:

  at var/cache/dev/srcDevDebugProjectContainerUrlMatcher.php:107
  at srcDevDebugProjectContainerUrlMatcher->match('/')
     (vendor/symfony/routing/Matcher/UrlMatcher.php:95)
  at SymfonyComponentRoutingMatcherUrlMatcher->matchRequest(object(Request))
     (vendor/symfony/routing/Router.php:262)
  at SymfonyComponentRoutingRouter->matchRequest(object(Request))
     (vendor/symfony/http-kernel/EventListener/RouterListener.php:114)
  at SymfonyComponentHttpKernelEventListenerRouterListener->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 SymfonyComponentEventDispatcherDebugWrappedListener->__invoke(object(GetResponseEvent), 'kernel.request', object(EventDispatcher))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:212)
  at SymfonyComponentEventDispatcherEventDispatcher->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 SymfonyComponentEventDispatcherEventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:139)
  at SymfonyComponentEventDispatcherDebugTraceableEventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor/symfony/http-kernel/HttpKernel.php:125)
  at SymfonyComponentHttpKernelHttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:66)
  at SymfonyComponentHttpKernelHttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:190)
  at SymfonyComponentHttpKernelKernel->handle(object(Request))
     (public/index.php:37)


    SymfonyComponentHttpKernelExceptionNotFoundHttpException:
No route found for "GET /" (from "http://localhost/my-project/")

  at vendor/symfony/http-kernel/EventListener/RouterListener.php:144
  at SymfonyComponentHttpKernelEventListenerRouterListener->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 SymfonyComponentEventDispatcherDebugWrappedListener->__invoke(object(GetResponseEvent), 'kernel.request', object(EventDispatcher))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:212)
  at SymfonyComponentEventDispatcherEventDispatcher->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 SymfonyComponentEventDispatcherEventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:139)
  at SymfonyComponentEventDispatcherDebugTraceableEventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor/symfony/http-kernel/HttpKernel.php:125)
  at SymfonyComponentHttpKernelHttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:66)
  at SymfonyComponentHttpKernelHttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:190)
  at SymfonyComponentHttpKernelKernel->handle(object(Request))
     (public/index.php:37)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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  
------ -------- -------- ------ ------

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.

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.

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 SymfonyComponentHttpKernelEventListener;
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());

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/                        
...

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.

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: The GET / route is not defined by default even though it might seem like it. You will need to add it yourself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...