Symfony 6中的路由注释不起作用

发布于 2025-02-10 05:41:11 字数 2005 浏览 2 评论 0原文

我正在Homestead上运行Symfony应用程序,但是当通过注释添加路由以进行简单操作时,它行不通。

namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class BlogController extends AbstractController
{
/**
 * @Route("/index",name="blog_index")
 */
public function index(){
    dd('Test KO');
  }
}

用于路线调试器

vagrant@homestead:~/code/symfony61$ php bin/console debug:router
 -------------------------- -------- -------- ------ ----------------------------------- 
  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_xdebug           ANY      ANY      ANY    /_profiler/xdebug                  
  _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   
 -------------------------- -------- -------- ------ ----------------------------------- 

i'm running a symfony application on Homestead, but when adding a route by annotation for simple action it doesn't work.

namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class BlogController extends AbstractController
{
/**
 * @Route("/index",name="blog_index")
 */
public function index(){
    dd('Test KO');
  }
}

for routes debugger

vagrant@homestead:~/code/symfony61$ php bin/console debug:router
 -------------------------- -------- -------- ------ ----------------------------------- 
  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_xdebug           ANY      ANY      ANY    /_profiler/xdebug                  
  _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   
 -------------------------- -------- -------- ------ ----------------------------------- 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相权↑美人 2025-02-17 05:41:12

Symfony 6不会带有预先配置的路由注释config。因此,您需要按照以下简单的步骤操作:

1-在config/routes文件夹下创建一个属性.yaml文件。

2-添加以下代码

controllers:
resource: ../../src/FOLDER_NAME_OF_WHERE_YOUR_ROUTER_CLASSES_EXISTS/
type: annotation

3-使用新语法添加您的路由注释,例如

#[Route('/inbound', name: 'inbound', methods: 'POST')]
public function inbound(Request $request): Response
{
    return new Response('OK', 200);
}

4-如果一切正常,请调试路线

bin/console debug:router

Symfony 6 is not coming with preconfigured route annotation config. So you need to follow this easy steps:

1- create a attributes.yaml file under config/routes folder.

2- add following code

controllers:
resource: ../../src/FOLDER_NAME_OF_WHERE_YOUR_ROUTER_CLASSES_EXISTS/
type: annotation

3- add your route annotation with new syntax like

#[Route('/inbound', name: 'inbound', methods: 'POST')]
public function inbound(Request $request): Response
{
    return new Response('OK', 200);
}

4- Debug your route if everything is ok

bin/console debug:router
眼睛会笑 2025-02-17 05:41:12

通常有一个文件:config/doutes.yaml,带有内容:

controllers:
    resource: ../src/Controller/
    type: annotation
    # (the type: annotation option also applies to attributes...)

请参阅: https://symfony.com/doc/current/routing.html#creating-routes-routes-as-attributes-or-antributes-or-annotations

There is usually a file: config/routes.yaml with the content:

controllers:
    resource: ../src/Controller/
    type: annotation
    # (the type: annotation option also applies to attributes...)

See: https://symfony.com/doc/current/routing.html#creating-routes-as-attributes-or-annotations

耳钉梦 2025-02-17 05:41:12

php8 + Symfony 6使用PHP注释:#[route('/',name:'app_index')]

PHP8 + Symfony 6 use PHP annotations: #[Route('/', name: 'app_index')]

北陌 2025-02-17 05:41:12

在Symfony 6和PHP 8中,您可以使用PHP属性而不是Symfony注释。这样使用。

#[Route('/index', name: 'blog_index')]
public function blog_index(){
    //code..
  }
}

In symfony 6 and PHP 8, You can use PHP attributes instead of symfony annotation. Use like this.

#[Route('/index', name: 'blog_index')]
public function blog_index(){
    //code..
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文