首页 > 解决方案 > 尽管在我调试时路由存在,但无法为路由生成 URL

问题描述

我提出了这个异常:

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.").

在我的树枝模板(包含 的行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>

虽然路线是正确的(我想)在我的 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()
        ]);
    }
}

该行php bin/console debug:router产生了这个输出,表明该路线确实存在:

 -------------------------- -------- -------- ------ ----------------------------------- 
  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                            
 -------------------------- -------- -------- ------ ----------------------------------- 

我仍然不明白为什么异常会无缘无故地被提出来,以至于这很烦人。我对 Twig 代码中的其他路由做了完全相同的事情,并且在我添加最后一个之前它工作得非常好。

这里的目标是能够停止出现此异常并让路由正常工作。

请帮助实现这一目标?如果您需要更多信息,请告诉我。

谢谢。

标签: phpsymfonyexceptionroutestwig

解决方案


没有什么能真正解决这个非常烦人的“问题”,我尝试多次清除缓存,它没有做任何事情。然后我看到有人说在安装apache-packwith composer 之后就成功了。我试过了,它奏效了......去看看。


推荐阅读