You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony 5.4如何在URL中传递.json作为后缀?

Symfony 5.4 路由配置需求

已尝试的路由配置

我尝试过以下两种路由配置:

第一种:

/**
    * @Route("/api/search.{format}", name="api_do_search")
    */ 

第二种:

@Route("/api/search.{_format}", name="api_do_search", defaults={"_format":"json"}, methods={"POST"}, requirements={"_format":"|json"})

期望效果

我希望使用URL http://localhost:8000/api/search.json,并且执行命令 php bin/console router:match /api/doSearch.json 后,能得到如下匹配结果:

php bin/console router:match /api/doSearch.json


                                                                                         
 [OK] Route "api_do_search" matches                                                      
                                                                                         

+--------------+----------------------------------------------------------+
| Property     | Value                                                    |
+--------------+----------------------------------------------------------+
| Route Name   | api_do_search                                            |
| Path         | /api/doSearch.{format}                                   |
| Path Regex   | {^/api/doSearch(?:\.(?P<format>[^/]++))?$}sDu            |
| Host         | ANY                                                      |
| Host Regex   |                                                          |
| Scheme       | ANY                                                      |
| Method       | ANY                                                      |
| Requirements | NO CUSTOM                                                |
| Class        | Symfony\Component\Routing\Route                          |
| Defaults     | _controller: App\Controller\TicketController::doSearch() |
|              | format: json                                             |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler  |
|              | utf8: true                                               |
+--------------+----------------------------------------------------------+

我使用的是Symfony 5.4版本。

内容的提问来源于stack exchange,提问作者Volatil3

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 02:32:03