Search code examples
phpsymfonysymfony4

Multiple routes in Symfony4 result in 404 error


I started a fresh symfony 4.2 website project.

I have created two simple controllers rendering two views.

The first one localhost/ works fine, however the second one localhost/home results in 404 error.

I don't understand what happens, everything is very simple and based on official documentation.


routes.yaml file:

index:
    path: /
    controller: App\Controller\IntroController::introView

home:
    path: /home
    controller: App\Controller\HomeController::homeView

IntroController.php file:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class IntroController extends AbstractController
{
    public function introView()
    {
        return $this->render('intro.html.twig');
    }
}

HomeController.php file:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class HomeController extends AbstractController
{
    public function homeView()
    {
        return $this->render('home.html.twig');
    }
}

php bin/console debug:router command:

 -------------------------- -------- -------- ------ ----------------------------------- 
  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   
  index                      ANY      ANY      ANY    /                                  
  home                       ANY      ANY      ANY    /home                              
 -------------------------- -------- -------- ------ ----------------------------------- 

apache sites-enabled.conf file:

...
    <VirtualHost *:80>
        ServerName localhostczy
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/czyjestemladna/public

        <Directory /var/www/czyjestemladna/public>
           Options Indexes FollowSymLinks
           AllowOverride All
           Require all granted
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

Solution

  • Maybe you forget to install apache pack?

    Try this command inside your project folder

    composer require symfony/apache-pack
    

    This will configure your .htaccess file inside public folder. Basically, when you request localhost\home, your Apache will looks at /var/www/czyjestemladna/public/home, which don't exist on your computer. So this line from the .htaccess file

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

    Will rewrite how Apache access the file, it will now call the index.php instead returning error 404