Symfony-docs: Symfony 4: No route found for "GET /" just after installation

Created on 31 Jan 2018  Â·  14Comments  Â·  Source: symfony/symfony-docs

I'm starting out with Symfony 4 following this guide https://symfony.com/doc/current/setup.html, so I have executed one after the other these commands:

  1. composer create-project symfony/website-skeleton my-project
  2. cd my-project
  3. composer require server --dev
  4. php bin/console server:run

but when I browse to http://localhost:8000 I get a No route found for "GET /" error.

Reviewed bug hasPR

Most helpful comment

That's expected, a new application has no default route. But there should be a nice page nonetheless displaying some information. And you can then continue creating your first page as described at https://symfony.com/doc/current/page_creation.html.

All 14 comments

I can reproduce this bug with the given steps. Now let's find the cause of it and fix it. Thanks!

I've just debug the error. The problem is that this code in PhpMatcherDumper.php never gets executed because $code is not empty:

        if ('' === $code) {
            $code .= "        if ('/' === \$pathinfo) {\n";
            $code .= "            throw new Symfony\Component\Routing\Exception\NoConfigurationException();\n";
            $code .= "        }\n";
        }

In my case, the $code variable contained:

PhpMatcherDumper.php on line 157:
"""
        if (0 === strpos($pathinfo, '/_')) {\n
            // _twig_error_test\n
            if (0 === strpos($pathinfo, '/_error') && preg_match('#^/_error/(?P<code>\\d+)(?:\\.(?P<_format>[^/]++))?$#s', $pathinfo, $matches)) {\n
                return $this->mergeDefaults(array_replace($matches, array('_route' => '_twig_error_test')), array (  '_controller' => 'twig.controller.preview_e â–¶
            }\n
\n
            // _wdt\n
            if (0 === strpos($pathinfo, '/_wdt') && preg_match('#^/_wdt/(?P<token>[^/]++)$#s', $pathinfo, $matches)) {\n
                return $this->mergeDefaults(array_replace($matches, array('_route' => '_wdt')), array (  '_controller' => 'web_profiler.controller.profiler:tool â–¶
            }\n
\n
            if (0 === strpos($pathinfo, '/_profiler')) {\n
                // _profiler_home\n
                if ('/_profiler' === $trimmedPathinfo) {\n
                    $ret = array (  '_controller' => 'web_profiler.controller.profiler:homeAction',  '_route' => '_profiler_home',);\n
                    if (substr($pathinfo, -1) !== '/') {\n
                        return array_replace($ret, $this->redirect($rawPathinfo.'/', '_profiler_home'));\n
                    }\n
\n
                    return $ret;\n
                }\n
\n
                if (0 === strpos($pathinfo, '/_profiler/search')) {\n
                    // _profiler_search\n
                    if ('/_profiler/search' === $pathinfo) {\n
                        return array (  '_controller' => 'web_profiler.controller.profiler:searchAction',  '_route' => '_profiler_search',);\n
                    }\n
\n
                    // _profiler_search_bar\n
                    if ('/_profiler/search_bar' === $pathinfo) {\n
                        return array (  '_controller' => 'web_profiler.controller.profiler:searchBarAction',  '_route' => '_profiler_search_bar',);\n
                    }\n
\n
                }\n
\n
                // _profiler_phpinfo\n
                if ('/_profiler/phpinfo' === $pathinfo) {\n
                    return array (  '_controller' => 'web_profiler.controller.profiler:phpinfoAction',  '_route' => '_profiler_phpinfo',);\n
                }\n
\n
                // _profiler_search_results\n
                if (preg_match('#^/_profiler/(?P<token>[^/]++)/search/results$#s', $pathinfo, $matches)) {\n
                    return $this->mergeDefaults(array_replace($matches, array('_route' => '_profiler_search_results')), array (  '_controller' => 'web_profiler. â–¶
                }\n
\n
                // _profiler_open_file\n
                if ('/_profiler/open' === $pathinfo) {\n
                    return array (  '_controller' => 'web_profiler.controller.profiler:openAction',  '_route' => '_profiler_open_file',);\n
                }\n
\n
                // _profiler\n
                if (preg_match('#^/_profiler/(?P<token>[^/]++)$#s', $pathinfo, $matches)) {\n
                    return $this->mergeDefaults(array_replace($matches, array('_route' => '_profiler')), array (  '_controller' => 'web_profiler.controller.prof â–¶
                }\n
\n
                // _profiler_router\n
                if (preg_match('#^/_profiler/(?P<token>[^/]++)/router$#s', $pathinfo, $matches)) {\n
                    return $this->mergeDefaults(array_replace($matches, array('_route' => '_profiler_router')), array (  '_controller' => 'web_profiler.controll â–¶
                }\n
\n
                // _profiler_exception\n
                if (preg_match('#^/_profiler/(?P<token>[^/]++)/exception$#s', $pathinfo, $matches)) {\n
                    return $this->mergeDefaults(array_replace($matches, array('_route' => '_profiler_exception')), array (  '_controller' => 'web_profiler.contr â–¶
                }\n
\n
                // _profiler_exception_css\n
                if (preg_match('#^/_profiler/(?P<token>[^/]++)/exception\\.css$#s', $pathinfo, $matches)) {\n
                    return $this->mergeDefaults(array_replace($matches, array('_route' => '_profiler_exception_css')), array (  '_controller' => 'web_profiler.c â–¶
                }\n
\n
            }\n
\n
        }\n
\n
"""

Maybe @yceruto, who created that code, can help us solve this problem. Thanks!

Sadly, the trick (upon installation) only works if there is no package that adds a route to the application, and that isn't the case for symfony/website-skeleton and its dev-requirement: symfony/profiler-pack -> symfony/web-profiler-bundle, symfony/twig-bundle

See recipe symfony/web-profiler-bundle, same if symfony/twig-bundle was installed.

Just thinking out loud: could we detect somehow in the dumper if there's any route associated with the / path and if there's none, add it like in the previous trick? Thanks!

@yceruto this super quick and dirty code solves the problem:

$homepageIsDefined = false;
foreach ($collection as $dumperRoute) {
    if ('/' === $dumperRoute->getRoute()->getPath()) {
        $homepageIsDefined = true;
        break;
    }
}

if (!$homepageIsDefined) {
    $code .= "        if ('/' === \$pathinfo) {\n";
    $code .= "            throw new Symfony\Component\Routing\Exception\NoConfigurationException();\n";
    $code .= "        }\n";
}

The same problem.

@yceruto I've proposed a simpler solution in https://github.com/symfony/symfony/pull/26041 Hopefully it will fix this issue.

I'm closing this issue because we are working on it on the code repository, which is where this can be fixed. Thanks!

I had the same issue. I fixed it by creating a controller and modifying the routes.yami file. This video shows the solution at around 11:30 https://www.youtube.com/watch?v=t5ZedKnWX9E

I'm not sure to understand the downvotes on this comment, the fix was merged and will be out in the next release, so this documentation will be fine.

same problem .. anything useful here ?

@Schrodinger0 What do you mean with "same problem"?

just after Installation
image

That's expected, a new application has no default route. But there should be a nice page nonetheless displaying some information. And you can then continue creating your first page as described at https://symfony.com/doc/current/page_creation.html.

Was this page helpful?
0 / 5 - 0 ratings