symfony 3和FOSRest捆绑路由

时间:2017-06-07 21:12:41

标签: symfony fosrestbundle symfony-routing

我正在尝试使用FOS REST Bundle设置symfony3代码库,但我最终没有找到路由错误

的appbundle \资源\配置\ API-的routing.yml

check:
    type: rest
    resource: "@AppBundle/Controller/CheckController.php"

应用\设置\ routing.yml中

app:
    type: rest
    prefix: /
    resource: '@AppBundle/Resources/config/api-routing.yml'

控制器

namespace AppBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\Version;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use Symfony\Component\HttpFoundation\Request;

/**
 * @RouteResource("Check")
 * @Version("v1")
 */

class CheckController extends FOSRestController
{

    public function getAction(Request $request)
    {
        $data = "Ok";
        return new JsonResponse($data);
    }
}

http://127.0.0.1:8000/v1/app/check/ return no route

我想整合rest api route的版本。你可以分享一下这个错误,我该如何纠正?

fos_rest:
    body_listener: true
    versioning:
        enabled: true
        resolvers:
            query: 
                enabled: true
                parameter_name: version
    format_listener:
        rules:
            - { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: false }
    param_fetcher_listener: true
    view:
        view_response_listener: 'force'
        formats:
            json: true

1 个答案:

答案 0 :(得分:0)

这是一个有效的例子。

应用\设置\ routng.yml

app:
    type: rest
    resource: "@AppBundle/Resources/config/routing.yml"
    prefix: /api

的appbundle \资源\配置\ routing.yml中

rest_my:
    type: rest
    prefix: /v1
    resource: "@AppBundle/Controller/MyRestController.php"
    name_prefix:  api_1_

有效路线将是:

http://127.0.0.1:8000/api/v1/<MyRestControllerAction>

或第二个控制器

的appbundle \资源\配置\ routing.yml中

rest_user:
    type: rest
    prefix: /v1/user
    resource: "@AppBundle/Controller/UserRestController.php"
    name_prefix:  api_1_

有效路线将是:

http://127.0.0.1:8000/api/v1/user/<UserRestControllerAction>

我希望我帮助你... Chears。

更新#1: 这是我对json格式的完整fos_rest配置到config.yml文件中:

fos_rest:
    routing_loader:
        default_format: json
        include_format: false
    body_converter:
        enabled: true
    body_listener:
        decoders:
            json: fos_rest.decoder.json
    format_listener:
        enabled: true
        rules:
            - { path: '^/api/v1/', priorities: ['json'], fallback_format: json, prefer_extension: true}
            - { path: '^/', priorities: ['json', 'html'], fallback_format: 'html' }
    serializer:
        serialize_null: true
    view:
        view_response_listener: 'force'
        templating_formats:
            html: false
    param_fetcher_listener: 'force'

如果您需要更多,那么您拥有fos_rest的完整文档 http://symfony.com/doc/current/bundles/FOSRestBundle/configuration-reference.html