8000 Symfony 4: No route found for "GET /" just after installation · Issue #9178 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Symfony 4: No route found for "GET /" just after installation #9178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zer0uno opened this issue Jan 31, 2018 · 14 comments
Closed

Symfony 4: No route found for "GET /" just after installation #9178

zer0uno opened this issue Jan 31, 2018 · 14 comments
Labels
bug hasPR A Pull Request has already been submitted for this issue. Status: Reviewed

Comments

@zer0uno
Copy link
Contributor
zer0uno commented Jan 31, 2018

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.

@javiereguiluz
Copy link
Member

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

@javiereguiluz
Copy link
Member

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!

@yceruto
Copy link
Member
yceruto commented Feb 1, 2018

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.

@javiereguiluz
Copy link
Member

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!

@javiereguiluz
Copy link
Member

@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";
}

@VolodymyrShaganian
Copy link
VolodymyrShaganian commented Feb 2, 2018

The same problem.

@javiereguiluz
Copy link
Member

@yceruto I've proposed a simpler solution in symfony/symfony#26041 Hopefully it will fix this issue.

@javiereguiluz javiereguiluz added the hasPR A Pull Request has already been submitted for this issue. label Feb 4, 2018
@javiereguiluz
Copy link
Member

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

fabpot added a commit to symfony/symfony that referenced this issue Mar 11, 2018
… (javiereguiluz)

This PR was squashed before being merged into the 3.4 branch (closes #26041).

Discussion
----------

Display the Welcome Page when there is no homepage defined

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony/symfony-docs#9178
| License       | MIT
| Doc PR        | -

In 3.4 we added a trick to display the Welcome Page when the user browses `/` and there are no routes defined. However, when using the `website-skeleton` (which is what most newcomers use ... and they are the ones that mostly need the "Welcome Page") the premise about *"no routes are defined"* is never true and the Welcome Page is never shown (see symfony/symfony-docs#9178 for one of the multiple error reports we've received).

So, I propose to make this change to always define the "Welcome Page" as the fallback:

* If no routes are defined for `/`, the Welcome Page is displayed.
* If there is a route defined for `/`, this code will never be executed because it's the last condition of the routing matcher.

Commits
-------

5b0d934 Display the Welcome Page when there is no homepage defined
symfony-splitter pushed a commit to symfony/http-kernel that referenced this issue Mar 11, 2018
… (javiereguiluz)

This PR was squashed before being merged into the 3.4 branch (closes #26041).

Discussion
----------

Display the Welcome Page when there is no homepage defined

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony/symfony-docs#9178
| License       | MIT
| Doc PR        | -

In 3.4 we added a trick to display the Welcome Page when the user browses `/` and there are no routes defined. However, when using the `website-skeleton` (which is what most newcomers use ... and they are the ones that mostly need the "Welcome Page") the premise about *"no routes are defined"* is never true and the Welcome Page is never shown (see symfony/symfony-docs#9178 for one of the multiple error reports we've received).

So, I propose to make this change to always define the "Welcome Page" as the fallback:

* If no routes are defined for `/`, the Welcome Page is displayed.
* If there is a route defined for `/`, this code will never be executed because it's the last condition of the routing matcher.

Commits
-------

5b0d934 Display the Welcome Page when there is no homepage defined
symfony-splitter pushed a commit to symfony/routing that referenced this issue Mar 11, 2018
… (javiereguiluz)

This PR was squashed before being merged into the 3.4 branch (closes #26041).

Discussion
----------

Display the Welcome Page when there is no homepage defined

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony/symfony-docs#9178
| License       | MIT
| Doc PR        | -

In 3.4 we added a trick to display the Welcome Page when the user browses `/` and there are no routes defined. However, when using the `website-skeleton` (which is what most newcomers use ... and they are the ones that mostly need the "Welcome Page") the premise about *"no routes are defined"* is never true and the Welcome Page is never shown (see symfony/symfony-docs#9178 for one of the multiple error reports we've received).

So, I propose to make this change to always define the "Welcome Page" as the fallback:

* If no routes are defined for `/`, the Welcome Page is displayed.
* If there is a route defined for `/`, this code will never be executed because it's the last condition of the routing matcher.

Commits
-------

5b0d9340d7 Display the Welcome Page when there is no homepage defined
@zacpattersondc
Copy link

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

@yceruto
Copy link
Member
yceruto commented Mar 19, 2018

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.

@Schrodinger0
Copy link

same problem .. anything useful here ?

@xabbuh
Copy link
Member
xabbuh commented Oct 10, 2018

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

@Schrodinger0
Copy link

just after Installation
image

@xabbuh
Copy link
Member
xabbuh commented Oct 10, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug hasPR A Pull Request has already been submitted for this issue. Status: Reviewed
Projects
None yet
Development

No branches or pull requests

8 participants
0