未捕获的PHP异常Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException:“未找到用于“ GET /”的路由

时间:2019-04-14 17:47:29

标签: php symfony4

我刚开始关注symfony 4教程 我使用composer设置了整个项目,并在浏览器http://127.0.0.1:8000/

中显示了索引文件

我被困在第一个路由器/控制器/视图示例上 路由无法正常运行或根本无法正常运行。我总是会收到404错误。

1:

enter image description here

2:

enter image description here

3:

enter image description here

<?php


namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class LuckyController
{
    public function number()
    {
        $number = random_int(0, 100);

        return new Response(
            '<html><body>Lucky number: '.$number.'</body></html>'
        );
    }
}

# the "app_lucky_number" route name is not important yet
app_lucky_number:
  path: /lucky/number
  controller: App\Controller\LuckyController::number

我没有主意,我在google上到处都没有找到解决方案。

1 个答案:

答案 0 :(得分:0)

确保您的控制器在正确的路径src/Controller/

示例

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class LuckyController extends AbstractController
{

  public function number()
  {
    echo __LINE__;die();
  }
}

检查config/routes.php文件是否具有正确的路由

namespace Symfony\Component\Routing\Loader\Configurator;

use App\Controller\LuckyController;

return function (RoutingConfigurator $routes) {
   // Matches /lucky exactly
   $routes->add('lucky_number', '/lucky')
      ->controller([LuckyController::class, 'number'])
   ;

};

OR

将您的YAML更改为

# the "app_lucky_number" route name is not important yet
app_lucky_number:
path: /lucky
controller: App\Controller\LuckyController::number

完成此访问后,访问此URL http://127.0.0.1:8000/lucky