【问题标题】:Symfony 4 / FosREST : API routes return 404 but not web routesSymfony 4 / FosREST:API 路由返回 404 但不是 web 路由
【发布时间】:2019-11-28 12:16:00
【问题描述】:

我正在建立一个多语言的经典 symfony 4.3 项目(又名网站),所有路线都可以。我想用一个简单的 API REST 公开一些数据(只有 3,4 个使用方法 GET 的路由),所以我安装了 FOSRestBundle 2.5。我知道这个版本与 SF 4.* 兼容。

问题是我的 API 路由返回所有 404 但不是“网络”路由。我使用 symfony doc 和 FOSREST doc 以及一些教程。 在 fos_rest.yaml 中,我在 rules 参数中声明了两种类型的路由。我已经在两个目录中分离了 web 控制器和 API 控制器(这两个目录挂载在 services.yaml 中)

这里是 FOS REST 配置: 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:

services:
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    App\ControllerApi\:
        resource: '../src/ControllerApi'
        tags: ['controller.service_arguments']

和注释定义: 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 控制器,这里是一个例子:

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);
    }
}

这是一个生成的路由示例(以主页为例):

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}/   

一切正常(以及其他网络路由)。

这里有 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

我没有发现错误。 感谢阅读和回答

【问题讨论】:

    标签: symfony4 fosrestbundle


    【解决方案1】:

    我找到了一个起始解决方案。我在 fos_rest.yaml 上更改了我的 conf:

    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 不在这里。

    控制器中的注解也有错误: 这是正确的语法:

    /**
         * @Rest\View()
         * @Rest\Get("/demo/{id}", name="api_demo")
         *
         * @param string $id
         *
         * @return View
         */
        public function getSimpleDataById(string $id): View
    

    【讨论】:

    • 并且控制器上的注释有错误:
    猜你喜欢
    • 1970-01-01
    • 2018-07-10
    • 2021-02-21
    • 2019-07-01
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    相关资源
    最近更新 更多