Symfony 4 / FosREST:API路由返回404,但不返回Web路由

时间:2019-07-19 13:28:20

标签: symfony4 fosrestbundle

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

问题是我的API路由返回了所有404但不是“ web”路由。我浏览了symfony文档和FOSREST文档以及一些教程。 在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

我没有找到错误。 感谢您的阅读和回答

1 个答案:

答案 0 :(得分:0)

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

TextField {
  border-bottom-width: 1;
  border-bottom-color: transparent;
}

我删除了规则“ ^ /”并添加了区域。 404不在这里。

控制器中的注释也出现错误: 这是正确的语法:

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