刚刚使用 symfony2 创建了一个新项目并安装了 FOSRestBundle。 Controller 示例:
<?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 .
关于php - Symfony2 FOSRestBundle handleView() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36038728/