symfony 注解中的路由问题

Posted

技术标签:

【中文标题】symfony 注解中的路由问题【英文标题】:Problem with routes in symfony annotations 【发布时间】:2021-06-26 16:41:09 【问题描述】:

我在 Symfony 中遇到了路由问题。我有它们用于注释。我创建了控制器:

php bin/console make: controller DefaultController

Symfony 的 localhost/.../public/index.php 页面向我显示了控制器,但如果我输入 localhost/.../public/index.php/welcome 它没有显示任何内容,我找不到错误

php bin/console debug:route
 -------------------------- -------- -------- ------ ----------------------------------- 
  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   
  autor                      ANY      ANY      ANY    /autor                             
  autor_list                 ANY      ANY      ANY    /autor/list                        
  autor_list_JSON            ANY      ANY      ANY    /autor/listJSON                    
  autor_new                  ANY      ANY      ANY    /autor/new                         
  home                       ANY      ANY      ANY    /welcome 

/auto、/auto/list、autor/listJSON、autor/new 路径也不起作用。

我的 routes.yaml

#index:
#    path: /
#    controller: App\Controller\DefaultController::index

我的控制器

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends AbstractController

    /**
    * @Route("/default", name="default")
    */
    public function index(): Response
    
    
        return $this->render('default/index.html.twig', [
            'controller_name' => 'DefaultController',
        ]);
    
    /**
    * @Route("/welcome", name="home")
    */
    public function welcome()
    
        $html = '<body>Hello world!</body>';
        return new Response($html);
    

【问题讨论】:

您使用的是什么网络服务器?是阿帕奇吗?看起来您没有设置 RewriteRules。见:symfony.com/doc/current/setup/… 我使用 Xampp。错误是:找不到“GET /welcome” Symfony\Component\HttpKernel\Exception\ NotFoundHttpException 的路由 删除问题然后重新发布在这附近确实被认为是一件非常糟糕的事情。 【参考方案1】:

删除 vendor 文件夹并再次生成:

composer install --ignore-platform-reqs

现在它可以正常工作了。 在 vendor 内部有一个不正确的实例。

重要,使用--ignore-platform-reqs生成

【讨论】:

以上是关于symfony 注解中的路由问题的主要内容,如果未能解决你的问题,请参考以下文章

Symfony:在其他功能中使用路由中的参数

为所有从包路由导入的前缀添加前缀时,如何在 Symfony 包的模板中生成 URL?

如何防止访问 Symfony 2 中的特定路由

Symfony 子域路由

根据 symfony 2.0 中的路由自定义 403 错误页面

从 Symfony2 中的 routing.yml 将一条路由重定向(301)到另一条路由