Symfony 4/FosREST:API 路由返回 404 但不返回 Web 路由 [英] Symfony 4 / FosREST : API routes return 404 but not web routes
问题描述
我正在建立一个多语言的经典 symfony 4.3 项目(又名网站),所有路径都可以.我想用一个简单的 API REST 公开一些数据(只有 3,4 条使用 GET 方法的路由),所以我安装了 FOSRestBundle 2.5.我知道此版本与 SF 4.* 兼容.
I'm setting up a classic symfony 4.3 project (aka a web site) in multi-languages and all routes are OK. I would like to expose some data with an simple API REST (only 3,4 routes with method GET) so i have installed FOSRestBundle 2.5. I know this version is compatible with SF 4.*.
问题是我的 API 路由返回所有 404 但不是web"路由.我阅读了 symfony doc 和 FOSREST doc 以及一些教程.在 fos_rest.yaml 中,我在 rules
参数中声明了两种类型的路由.我已经在两个目录中分离了 web 控制器和 API 控制器(这两个目录都挂载在 services.yaml 中)
The problem is my API routes return all 404 BUT NOT the "web" routes. I fllow symfony doc and FOSREST doc and some tutorials.
In fos_rest.yaml, i have declared the two types of routes in rules
argument. I have separeted the controllers for web and controller for API in two directories (the two directories are mount in services.yaml)
这里是 FOS REST 配置:配置/包/fos_rest.yaml
Here the FOS REST configuration : config/packages/fos_rest.yaml
fos_rest:
routing_loader:
default_format: json
include_format: false
body_listener: true
format_listener:
rules:
- { path: '^/api', priorities: [ json ], fallback_format: json, prefer_extension: false }
- { path: '^/', priorities: [ 'html', '*/*'], fallback_format: ~, prefer_extension: true }
param_fetcher_listener: true
access_denied_listener:
json: true
view:
view_response_listener: 'force'
formats:
json: true
config/services.yaml:
config/services.yaml:
services:
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
App\ControllerApi\:
resource: '../src/ControllerApi'
tags: ['controller.service_arguments']
和注解定义:config/routes/annotations.yaml
And annotations definitions : config/routes/annotations.yaml
# https://symfony.com/doc/current/routing.html#localized-routing-i18n
web_controllers:
resource: ../../src/Controller/
type: annotation
prefix:
en: /
fr: /{_locale}
de: /{_locale}
es: /{_locale}
pt: /{_locale}
requirements:
_locale: '%app_locales%'
defaults:
_locale: '%locale%'
# Controller for Rest API
rest_controller:
resource: ../../src/ControllerApi/
type: annotation
prefix: /api
Web 控制器是基础和扩展 AbstractController
.对于 API 控制器,这里有一个例子:
Web controllers are basics and extends AbstractController
. For API controller, here an exemple :
namespace App\ControllerApi;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
final class fooApiController extends AbstractFOSRestController
{
/**
* @param string $id
*
* @return View
*
* @Rest\Get("/demo/{$id}", name="api_demo")
*/
public function getSimpleDataById($id)
{
/** @var Dso $dso */
$data = $this->myCustomManager->getData($id);
if (!$data instanceof CustomEntity) {
throw new NotFoundHttpException();
}
return View::create($data, Response::HTTP_OK);
}
}
这是生成的路由示例(带有主页的示例):
Here's an exemple of route generated (example with homepage) :
www-data@php:~/symfony$ php bin/console debug:router | grep home
homepage.en ANY ANY ANY /
homepage.fr ANY ANY ANY /{_locale}/
homepage.de ANY ANY ANY /{_locale}/
homepage.es ANY ANY ANY /{_locale}/
homepage.pt ANY ANY ANY /{_locale}/
一切正常(还有其他网络路由).
All is ok (and others routes for web too).
API 如下:
www-data@php:~/symfony$ php bin/console debug:router | grep api
api_demo GET ANY ANY /api/demo/{$id}
所以我卷曲了两个:
curl --head http://symfony.local
HTTP/1.1 200 OK
Server: nginx
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.2.14
Set-Cookie: PHPSESSID=336b94195892ebb5a3b4e7f2d2799fbb; path=/; HttpOnly; SameSite=lax
Cache-Control: max-age=0, must-revalidate, private, s-maxage=3600
Date: Fri, 19 Jul 2019 13:26:05 GMT
X-Debug-Token: 5653c7
# Works with others URL...
curl --head http://symfony.local/api/demo/m42
HTTP/1.1 404 Not Found
Server: nginx
Content-Type: application/json
Connection: keep-alive
X-Powered-By: PHP/7.2.14
Cache-Control: max-age=0, must-revalidate, private
Date: Fri, 19 Jul 2019 13:27:03 GMT
我没有发现错误.感谢阅读和回答
I don't find the error. Thanks for reading and answering
推荐答案
我找到了一个初始解决方案.我已经更改了 fos_rest.yaml 上的配置:
I have found a starting solution. I have changed my conf on fos_rest.yaml :
fos_rest:
routing_loader:
default_format: json
include_format: false
body_listener: true
format_listener:
rules:
- { path: '^/api', priorities: [ json ], fallback_format: json, prefer_extension: true }
param_fetcher_listener: true
access_denied_listener:
json: true
view:
view_response_listener: true
formats:
json: true
zone:
- { path: ^/api/* }
我删除了规则^/"并添加了区域.而 404 不存在.
I have removed the rule '^/' and adding the zone. And the 404 wasn't here.
控制器中的注解也有错误:这是正确的语法:
There was an error too on annotation in controller: Here's the correct syntax :
/**
* @Rest\View()
* @Rest\Get("/demo/{id}", name="api_demo")
*
* @param string $id
*
* @return View
*/
public function getSimpleDataById(string $id): View
这篇关于Symfony 4/FosREST:API 路由返回 404 但不返回 Web 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!