routing - 找不到 Symfony 3 路由

标签 routing symfony

尝试遵循 Symfony 3.1 书:

这是我的 Controller

<?php
namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class LuckyController
{
    /**
     * @Route("/lucky/number")
     */
    public function numberAction() {
        $num = rand(0,100);
        return new Response("number is ".  $num);
    }
}

该书转到以下网址:http://localhost:8000/lucky/number
查看响应。然而,对我来说,这以 404 The requested resource /lucky/number was not found on this server. 结束。 .

从阅读其他一些类似的问题来看,路由似乎可以通过注释来完成。这里的代码有什么问题?

编辑:关于网络服务器:我正在从 web/目录运行内置的 php 服务器。即 php -S localhost:8000 .也跑了 php bin/console server:run - 发回 No route found for "GET /lucky/number

最佳答案

这里还有另一个可能的问题,那就是缓存的存在。如果您正在学习本教程,您可能已经看过第一页,因此 Symfony 缓存了类和路由。
为了确保 Symfony 接受这些更改,请确保清除缓存(使用正确的环境参数):

php bin/console cache:clear --no-warmup -e prod

关于routing - 找不到 Symfony 3 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38111861/

相关文章:

angularjs - 为单页应用程序 (SPA) 创建一个链接以登陆不同的选项卡

.net-core - 在 ASP.Net Core 3 中使用 IRouteConstraint 捕获所有路由

mysql - Doctrine - 获取下一条和上一条记录

forms - 如何在 symfony2 中使用 Lexik Form Filter bundle 过滤日期?

symfony - 如何使用 Assets 在 Symfony2 + Twig 中渲染(未优化的)图像标签

macos - 是否有针对在 MacOS 上设置 net.inet.ip.scopedroute=0 导致的 ARP 条目损坏的解决方法?

http - Express - 如何阻止无效的 http 方法?

binding - 在 Route 中使用 Play Framework : Reverse routing 中的对象

javascript - 使用表行索引填充 Symfony 表单行

Twig 提取 FOR 循环变量