【发布时间】: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