【问题标题】:Problem with routes in symfony annotationssymfony 注解中的路由问题
【发布时间】: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 的路由
  • 删除问题然后重新发布在这附近确实被认为是一件非常糟糕的事情。

标签: php symfony routes annotations url-routing


【解决方案1】:

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

composer install --ignore-platform-reqs

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

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

【讨论】:

    猜你喜欢
    • 2019-06-13
    • 1970-01-01
    • 2019-10-20
    • 2017-09-10
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多