尽管在调试时存在路由,但无法生成路由的URL

时间:2020-05-18 16:57:37

标签: php symfony exception routes twig

我引发了这个异常:

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中用Annotation正确声明了该路线:

<?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代码中存在的其他路由做了完全相同的操作,并且在我添加最后一个之前,它的工作情况非常好。

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

有帮助实现这一目标吗?让我知道是否需要其他信息。

谢谢。

1 个答案:

答案 0 :(得分:0)

没有什么能真正解决这个非常烦人的“问题”,我多次尝试清除缓存,但是它什么也没做。然后我看到有人说,用作曲家安装pip uninstall flask pip uninstall flask_cors pip3 install flask pipenv shell pipenv install flask_cors pip install pipreqs git add . git commit -am "New cor resolved" git push heroku master 之后就成功了。我试过了,行得通。