Routing not working in Symfony 3.4
Asked Answered
P

4

7

I have created a new Symfony 3.4 project using:

composer create-project symfony/skeleton my-project

After that I added the following components:

composer require twig
composer require annotations
composer require maker

And created a Controller:

php bin/console make:controller

I added an action with a route "legal". Here is the DefaultController:

<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function index()
    {
        return $this->render('index.html.twig', [
            'controller_name' => 'DefaultController',
        ]);
    }

    /**
     * @Route("/legal", name="legal")
     */
    public function legal()
    {
        return $this->render('legal.html.twig', []);
    }
}

File config/routes.yaml:

#index:
#    path: /
#    defaults: { _controller: 'App\Controller\DefaultController::index' }

And config/routes/annotations.yaml:

controllers:
    resource: ../../src/Controller/
    type: annotation

When I access the homepage, no problem, the page is showing. But when I try the /legal page, I have a 404 :

Not Found - The requested URL /legal was not found on this server.

php bin/console debug:router shows the expected:

 ------------------ -------- -------- ------ -------------------------- 
  Name               Method   Scheme   Host   Path                      
 ------------------ -------- -------- ------ -------------------------- 
  homepage           ANY      ANY      ANY    /                         
  legal              ANY      ANY      ANY    /legal                    
  _twig_error_test   ANY      ANY      ANY    /_error/{code}.{_format}  
 ------------------ -------- -------- ------ -------------------------- 

I cleared the cache, with the console command and by removing the content of the var/cache directory. But still the 404.

I'm new to 3.4. Any ideas ? Thanks...

Postal answered 9/3, 2018 at 17:52 Comment(4)
Are you using the built-in PHP server or are you serving this through Apache?Suppose
And does your / route work ?Vertigo
What if you use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route instead of Symfony\Component\Routing\Annotation\Route ?Chandra
@jljohnstone: I'm serving the site through Apache, and @Antoine Galluet: yes, the / route works. I figured out that I had forgotten the .htaccess, which is not created automatically by Symfony/Flex (see my answer).Postal
P
15

Well, as @Basel Issmail pointed out, Symfony/Flex doesn't create a .htaccess like the previous Symfony installer did, and I had forgotten it.

I just had a minimal Apache configuration file:

<VirtualHost *:80>
    DocumentRoot /path/to/my-project/public
    ServerName myproject.localhost

    <Directory /path/to/my-project/public>
        Options -Indexes +FollowSymLinks -MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

So I created the .htaccess file in /public/ (where the index.php file lies), and the minimal required configuration is something like:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>

The missing part was to rewrite all queries (except existing files like assets) to index.php, for Symfony to handle the route.

--- Edit ---

Instead of manually creating the .htaccess, you can also just use a symfony flex recipe:

composer require apache-pack

This will install this .htacess file.

Postal answered 10/3, 2018 at 13:55 Comment(0)
P
5

Sounds like you need to setup your RewriteBase with apache, since the problem is not at /

Maybe .htaccess file is even missing since you are using Symfony/flex, and it doesn't add it by default like previous Symfony editions.

Check this page to configure a Web Server

.htaccess should be inside public folder and The minimum configuration to get your application running under Apache is:

<VirtualHost *:80>
    ServerName domain.tld
    ServerAlias www.domain.tld

    DocumentRoot /var/www/project/public
    <Directory /var/www/project/public>
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/project>
    #     Options FollowSymlinks
    # </Directory>

    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
Placid answered 10/3, 2018 at 0:44 Comment(1)
Thanks for your answer, it pointed me to the right direction; I had forgotten the .htaccess. But the important part was the rewriting rules to redirect all requests to index.php, so I wrote a complete answer.Postal
G
0

Did you load your Bundle "App" in the AppKernel.php ? There is a Array where you musst Open Add the AppBundle.php of your Bundle.

$bundles = [
/**lot of other bundles*//
new App\AppBundle() 
]

It musst look Like this

Greenland answered 9/3, 2018 at 23:9 Comment(1)
Thanks, but in my case it is not necessary, as I have not created an AppBundle (all source code is directly under the /src/ directory).Postal
D
0

Got same issue with symfony install on Synology DSM 6.2. I confirm, just run "composer require apache-pack" as mentioned by scandel, and annotations routing will work great. The .htaccess file with rewrite cond will be automatically created in the "public" folder.

Thank you ! :)

Here the .htaccess file created by the symfony flex install :

#----------------------------
# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex index.php

# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
# Options FollowSymlinks

# Disabling MultiViews prevents unwanted negotiation, e.g. "/index" should not resolve
# to the front controller "/index.php" but be rewritten to "/index.php/index".
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the index.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by Apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/index.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 307 ^/$ /index.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>
#----------------------------
Daryn answered 15/2, 2019 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.