我正在为 REST 我正在使用FOSRestBundle
进行一些 Rest 和一些 SOAP 服务 SOAP 类型我正在使用BeSimple Soap Bundle
(这实际上并不相关,因为问题在于FOSRest
侦听器 IMO ,但我说它只是以防万一)。
问题在于FOSRest
提供了一个监听器来处理响应,并像这样序列化/呈现它们:
<?php
/**
* @Rest\View
*/
public function getAction()
{
return $item;
}
它处理将序列化为JSON/XML
或其他任何内容的响应。
当我尝试调用我的SOAP服务并且监听器对我说它不支持SOAP
作为格式(它支持JSON,XML等)时会发生冲突。
当我将view_response_listener: 'force'
放入我的FOSRest yml配置文件时会发生这种情况。如果我将其更改为view_response_listener: 'enabled'
,SOAP服务确实有效,但我似乎失去了自动处理响应的方便FOSRest能力。
我怎样才能让FOSRest view_response_listener
只处理正确的回复,让SOAP Bundle
处理肥皂类型的回复。
编辑:
fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener: true
view:
view_response_listener: 'force' #This is the parameter that is driving me crazy. Value to 'force' Rest controllers work just fine SOAP don't, value to 'enabled' the other way around
formats:
xml: true
json : true
templating_formats:
html: true
force_redirects:
html: true
failed_validation: HTTP_BAD_REQUEST
default_engine: twig
routing_loader:
default_format: json
这是FOSRestBundle
配置。
以下是 Rest 端点和 SOAP 端点的routing.yml
:
#config/routing.yml
rest_api:
resource: "@RESTBundle/Resources/config/api_routes.yml"
host: "%host_front%" #api.myproject.local
type: rest
#api_routes.yml detail
accounts:
resource: 'RESTBundle\Controller\AccountsController'
type: rest
name_prefix: api_
#config/routing.yml
soap_api:
resource: "@SOAPBundle/Resources/config/soap_routing.yml"
host: "%host_ws%"
prefix: "%contexto_ws%"
#soap_routing.yml detail
_besimple_soap:
resource: "@BeSimpleSoapBundle/Resources/config/routing/webservicecontroller.xml"
soap_ws:
resource: "@SOAPBundle/Controller/"
type: annotation
RestController示例:
<?php
namespace RESTBundle\Controller\API;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc as ApiDoc;
use Symfony\Component\HttpFoundation\Request;
/**
* @Rest\RouteResource("accounts", pluralize=false)
*/
class AccountsController extends FOSRestController
{
/**
* @Rest\View
*/
public function getAction($userId)
{
return [
['name' => 'Example', 'pass'=> 'example'],
['name' => 'Example2', 'pass'=> 'example2']
];
}
}
SOAP控制器:
/**
* @Soap\Header("user", phpType="string")
* @Soap\Header("token", phpType="string")
*/
class AccountsController implements ContainerAwareInterface
{
use ContainerAwareTrait;
/**
* @Soap\Method("getAccounts")
* @Soap\Param("account", phpType="SOAPBundle\DataType\AccountFilter" )
*
* @Soap\Result(phpType="SOAPBundle\DataType\Account[]"")
*/
public function getAccountsAction(Request $request, AccountFilter $filter)
{
return [(new Account())->setName('Example')->setPass('example')] //This is just an example
}
}
使用错误编辑:
request.ERROR:未捕获的PHP异常 Symfony的\分量\ HttpKernel \异常\ UnsupportedMediaTypeHttpException: “格式'肥皂'不受支持,处理程序必须实现”at /home/teampro/Sites/corinto/vendor/friendsofsymfony/rest-bundle/View/ViewHandler.php 第292行{“例外”:“[对象] (Symfony的\元器件\ HttpKernel \异常\ UnsupportedMediaTypeHttpException(代码: 0):不支持格式'soap',处理程序必须在 /.../vendor/friendsofsymfony/rest-bundle/View/ViewHandler.php:292)“} []
答案 0 :(得分:1)
您可以在fos_rest配置中定义如何像this那样收听规则:
format_listener:
rules:
- { path: ^/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true } # default routes
- { path: '/soap', priorities: [xml], fallback_format: xml, prefer_extension: true } # your soap route
- { path: '^/rest', priorities: [xml, json], fallback_format: xml, prefer_extension: true } # your rest route
希望这有帮助!!
答案 1 :(得分:0)
首先,明确地将路由资源与prefix
属性
# app/config/routing.yml
_besimple_soap:
resource: "@BeSimpleSoapBundle/Resources/config/routing/webservicecontroller.xml"
prefix: "%contexto_ws%"
rest_api:
resource: "@RESTBundle/Resources/config/api_routes.yml"
host: "%host_front%" #api.myproject.local
prefix: /rest
type: rest
现在看起来你的REST控制器处理WS路由,因为它与configuraiton重叠。使用前缀分隔路径名称空间可以提供帮助。
其次,验证请求主机,因为您的配置已根据请求主机变量
拆分路由第三步,使用
检查路由配置bin/console debug:router
bin/console router:match