dou31797719 2019-04-14 17:47
浏览 1489

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

I just started following a symfony 4 tutorial I set up the whole project with composer and got the index file showing up in browser http://127.0.0.1:8000/

I'm stuck on the very first Router/Controller/View example The route is not working properly or at all. I always get a 404 error.

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

I'm out of ideas, I've looked everywhere on google and did not found a solution.

  • 写回答

1条回答 默认 最新

  • douxiangui5011 2019-04-14 17:57
    关注

    Make sure your contorller is on the right path src/Controller/

    Example

    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();
      }
    }
    

    Check the config/routes.php files have the correct routes

    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

    Change your YAML to

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

    After doing this access this URL http://127.0.0.1:8000/lucky

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大