仅为应用程序中的特定包调整bundle配置

时间:2012-04-05 08:34:48

标签: rest symfony configuration bundle

我有使用FOSRestBundle的Symfony应用程序,并且有一些 在main config.yml中配置的设置:

fos_rest: 
    service: 
        serializer: my.serializer 
        view_handler: my.viewHandler 
    view: 
        view_response_listener: false 
        failed_validation: 200 

现在我正在构建新的公共API包,我有以下问题。 当我从浏览器测试我的API时,我得到了

Unable to find template "". 
500 Internal Server Error - InvalidArgumentException

它起作用 卷曲。我猜它失败了因为浏览器要求输入html内容类型,并且 我不需要任何模板,我只想像这样返回xml / json:

return $this->get('fos_rest.view_handler')->handle(View::create(array('x' => 'y'))); 

我用更新的配置修复此问题:

fos_rest: 
    service: 
        serializer: my.serializer 
        view_handler: my.viewHandler 
    view: 
        view_response_listener: false 
        failed_validation: 200 
    format_listener: 
       default_priorities: [xml, json] 
       fallback_format: xml 

然后我在推出应用程序(其他捆绑包)中打破了一些操作 它使用FOSRestBundle)。 有没有办法调整FOSRestBundle(或任何其他捆绑)配置 仅适用于我的公共APi套装? 我尝试使用Extension:

$loader = new YmlFileLoader($container, new FileLocator(__DIR__.'/../ 
Resources/config')); 
        $loader->load('services.yml'); 

其中services.yml是:

fos_rest: 
  service: 
    view_handler: my.public.api.view_handler 
  format_listener: 
    default_priorities: [xml, json] 
    fallback_format: xml 

但我得到了:

InvalidArgumentException: There is no extension able to load the 
configuration for "fos_rest". 

2 个答案:

答案 0 :(得分:4)

当您通过composer安装软件包时,您需要手动将软件包添加到app / AppKernel.php:

public function registerBundles()
{
    $bundles = array(
        // ...
        new FOS\RestBundle\FOSRestBundle(),
        new JMS\SerializerBundle\JMSSerializerBundle(),
        // ...
    );
}

Here is more info.

答案 1 :(得分:0)

当我们在安装rest api软件包后使用symfony4 + rest api时,检查fosrest bunfle是否未退出的捆绑文件,然后添加 FOS \ RestBundle \ FOSRestBundle :: class => ['all'=> true]