【问题标题】:Unable to generate a URL for a route although the route is present when I debug尽管在我调试时路由存在,但无法为路由生成 URL
【发布时间】:2020-05-18 16:57:37
【问题描述】:

我提出了这个异常:

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

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

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

谢谢。

【问题讨论】:

  • 这可能是半魔法清除缓存的时间。
  • 做了多次,没有成功,我发现有人说用 composer 安装 apache-pack 会有所帮助。这样做了,它又起作用了……这真是令人讨厌……@Cerad
  • Apache 包?现在那将是神奇的。运行作曲家本身几乎不可能做某事。很高兴你成功了。

标签: php symfony exception routes twig


【解决方案1】:

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

【讨论】:

  • FWIW,您可以将添加的...public/.htaccess中的元素复制到http_vhosts.confyoursite.conf中的适当位置并删除.htaccess。工作得很好。
【解决方案2】:

不添加apache-pack 的等效解决方案是在站点的http-vhosts.confyoursite.conf 中使用以下内容(基于先前推荐的.htaccess)。 :

    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>

【讨论】:

    猜你喜欢
    • 2015-01-16
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2016-11-03
    • 1970-01-01
    • 2013-07-07
    相关资源
    最近更新 更多