VDS-Atomic
VDS-Atomic

Reputation: 115

Unable to generate a URL for a route although the route is present when I debug

I have this exception being raised :

RouteNotFoundException
RuntimeError
HTTP 500 Internal Server Error
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "listecours" as such route does not exist.").

Raised at this point of my twig template (the line containing path('listecours')) :

                       </li>
                        <li class="nav-item active">
                            <a href="{{ path('listexercices') }}" class="nav-link">Liste d'exercices</a>
                        </li>
                        <li class="nav-item active">
                            <a href="{{ path('listecours') }}" class="nav-link">Liste des cours</a>
                        </li>
                    {% else %}
                        <li class="nav-item active">
                            <a href="{{ path('listexercices') }}" class="nav-link">Liste d'exercices</a>
                        </li>

Although the route is correctly (I suppose) declared with an Annotation in my CoursController:

<?php

namespace App\Controller;

use App\Repository\CoursRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

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

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

The line php bin/console debug:router produced this output, showing that the route is indeed present :

 -------------------------- -------- -------- ------ ----------------------------------- 
  Name                       Method   Scheme   Host   Path                               
 -------------------------- -------- -------- ------ ----------------------------------- 
  _preview_error             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   
  cours                      ANY      ANY      ANY    /cours                             
  listecours                 ANY      ANY      ANY    /listecours                        
  exercices                  ANY      ANY      ANY    /exercices                         
  listexercices              ANY      ANY      ANY    /listexercices                     
  main                       ANY      ANY      ANY    /main                              
  prof                       ANY      ANY      ANY    /prof                              
  listetudiants              ANY      ANY      ANY    /listetudiants                     
  registration               ANY      ANY      ANY    /registration                      
  app_login                  ANY      ANY      ANY    /                                  
  app_logout                 ANY      ANY      ANY    /logout                            
 -------------------------- -------- -------- ------ ----------------------------------- 

I still don't understand why the exception keep getting raised for no apparent reason, to a point that it's just annoying. I did the exact same thing for the other routes present in the Twig piece of code I did, and it worked perfectly fine until I added this last one.

The goal here is to be able to stop having this exception and have the route working.

Anyhelp to achieve that please? Let me know if you want additional information.

Thanks.

Upvotes: 0

Views: 1928

Answers (3)

Double J
Double J

Reputation: 31

Take note that this error may be generated upon faulty code. Then every time you start Symfony by using symfony server:start, symfony is printing part of the log file(?). The displayed log statements DO NOT necessarily apply to the current start up of the symfony development server. The log statements may be exceptions that were thrown during a previous run. I just found this out when trouble shooting the above mentioned error. I removed all routes, but one from the entire code base, cleansed all caches and still had the same error. Then I realised that the log I was watching was old. Just wasted some hours on this....

Upvotes: 0

geoB
geoB

Reputation: 4704

An equivalent solution without adding apache-pack is to use the following (based on a previously recommended .htaccess) in the site's http-vhosts.conf or yoursite.conf. :

    ServerName a.good.name
    DocumentRoot "..."
    <Directory "...">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
# added by apache-pack, part 1
            RewriteRule .* - [E=BASE:%1]
            RewriteCond %{HTTP:Authorization} .+
            RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
            RewriteCond %{ENV:REDIRECT_STATUS} =""
            RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ %{ENV:BASE}/index.php [L]
# end, added by apache-pack, part 1
        </IfModule> 
    </Directory>
# added by apache-pack, part 2
    <IfModule !mod_rewrite.c>
        <IfModule mod_alias.c>
            RedirectMatch 307 ^/$ /index.php/
            # RedirectTemp cannot be used instead
        </IfModule>
    </IfModule>
# end, added by apache-pack, part 2
    <IfModule dir_module>
        DirectoryIndex index.php
    </IfModule>

Upvotes: 0

VDS-Atomic
VDS-Atomic

Reputation: 115

Nothing really solved this very annoying "issue", I tried clearing the cache multiple times, it didn't do anything. Then I saw someone saying that after installing the apache-pack with composer did the trick. I tried, it worked ... Go figure.

Upvotes: 1

Related Questions