Symfony4:找不到“ GET / lucky / number”的路线

sen*_*rio 4 php controller symfony symfony-routing symfony4

我开始玩symfony4。我刚刚创建了新的应用程序并创建了新的LuckyController。它与以这种方式配置的route.yaml一起使用:

lucky:
    path: /lucky/number
    controller: App\Controller\LuckyController::number
Run Code Online (Sandbox Code Playgroud)

使用以下控制器:

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class LuckyController
{
    public function number()
    {
        return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我想使用注释。所以我决定评论routes.yaml。以下文档解释了如何在symfony中创建路由,我已经做到了:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class LuckyController extends Controller
{
    /**
     * @Route("/lucky/number")
     */
    public function number()
    {
        return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
    }
}
Run Code Online (Sandbox Code Playgroud)

找不到路线

Bha*_*idi 9

有时会由于缓存而出现此问题。您需要运行php bin/console cache:clear。然后它将正常运行。


Ima*_*iev 5

在Symfony4中,您必须安装annotations捆绑软件。

运行此命令 composer require annotations

然后重新启动您的项目。