编程语言
首页 > 编程语言> > PHP-Symfony2 FOSRestBundle handleView()不起作用

PHP-Symfony2 FOSRestBundle handleView()不起作用

作者:互联网

刚刚使用symfony2创建了一个新项目,并安装了FOSRestBundle.
控制器示例:

<?php

namespace ApiBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\FOSRestController;


class TestController extends FOSRestController
{
    public function TestAction()
    {
        $data = array ('1', '2', '3', 'four'); // get data, in this case list of users.
        $view = $this->view($data, 200)
            ->setTemplate("ApiBundle:Test:test.html.twig")
            ->setTemplateVar('test')
        ;

        return $this->handleView($view);       
    }

}

得到错误消息:

You have requested a non-existent service "fos_rest.view_handler".

有人对此有想法吗?

解决方法:

在您的AppKernel中,请确保您具有以下内容:

// app/config/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new FOS\RestBundle\FOSRestBundle(),
    );
}

您肯定忘记了this step.

标签:fosrestbundle,symfony,php
来源: https://codeday.me/bug/20191118/2031965.html