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

问题描述 投票:0回答:3

我提出了这个例外:

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 代码段中存在的其他路线做了完全相同的事情,并且它工作得很好,直到我添加了最后一个。

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

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

谢谢。

php symfony exception routes twig
3个回答
1
投票

没有什么能真正解决这个非常烦人的“问题”,我多次尝试清除缓存,但没有做任何事情。然后我看到有人说用composer安装了

apache-pack
后就成功了。我试过了,它成功了……想想吧。


0
投票

不添加

apache-pack
的等效解决方案是在站点的
.htaccess
http-vhosts.conf
中使用以下内容(基于之前推荐的
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>

0
投票

请注意,此错误可能是由于代码错误而产生的。然后每次使用 symfony server:start 启动 Symfony 时,symfony 都会打印部分日志文件(?)。显示的日志语句不一定适用于 symfony 开发服务器的当前启动。日志语句可能是上次运行期间引发的异常。 我是在解决上述错误时才发现这一点的。我删除了所有路由,但从整个代码库中删除了一条路由,清理了所有缓存,但仍然出现相同的错误。然后我意识到我正在看的日志已经很旧了。只是在这上面浪费了一些时间......

© www.soinside.com 2019 - 2024. All rights reserved.