Php 无法使用FOSRestBundle
我正试图使用Symfony2和FOSRestBundle来设计一个REST框架,但我失败得很惨 我已经做了以下工作: 在我的deps文件中:Php 无法使用FOSRestBundle,php,rest,symfony,fosuserbundle,Php,Rest,Symfony,Fosuserbundle,我正试图使用Symfony2和FOSRestBundle来设计一个REST框架,但我失败得很惨 我已经做了以下工作: 在我的deps文件中: [FOSRest] git=git://github.com/FriendsOfSymfony/FOSRest.git target=fos/FOS/Rest [FOSRestBundle] git=git://github.com/FriendsOfSymfony/FOSRestBundle.git target=bun
[FOSRest]
git=git://github.com/FriendsOfSymfony/FOSRest.git
target=fos/FOS/Rest
[FOSRestBundle]
git=git://github.com/FriendsOfSymfony/FOSRestBundle.git
target=bundles/FOS/RestBundle
[JMSSerializerBundle]
git=git://github.com/schmittjoh/JMSSerializerBundle.git
target=bundles/JMS/SerializerBundle
在我的应用程序/config.yml中
fos_rest:
view:
formats:
rss: true
xml: false
templating_formats:
html: true
force_redirects:
html: true
failed_validation: HTTP_BAD_REQUEST
default_engine: twig
sensio_framework_extra:
view:
annotations: false
在我的控制器中:
namespace Rest\WebServiceBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\View\View;
class DefaultController extends Controller
{
public function indexAction($name)
{
$view = View::create()
->setStatusCode(200)
->setData($name);
return $this->get('fos_rest.view_handler')->handle($view);
}
}
当我转到URL时:
我得到:
Unable to find template "".
500 Internal Server Error - InvalidArgumentException
2 linked Exceptions: Twig_Error_Loader » Twig_Error_Loader
文档对我来说似乎很混乱,我无法继续。我所希望的就是能够将一个数据数组传递给控制器并返回JSON格式。有人能帮忙吗?在
config.yml
的格式部分,您必须启用json格式,禁用其他格式,并在路由中将默认的\u格式
值设置为json。e、 g
# app/config/config.yml
fos_rest:
view:
formats:
json: true
rss: false # removing them will also work
xml: false
#.......
#bundle/routing.yml
route_name:
pattern: /route
defaults: { _controller: Bundle:Controller:Method, _format:json }
或者,在控制器中,您可以执行以下操作
$view->setFormat('json');
另外,请检查文档中给出的示例链接。我也很难做到这一点。对于看似相对简单的任务来说,这似乎相当令人困惑。你在这方面运气好吗?它对我也很好,但只有当我使用数组作为数据时,如果我想输出一个对象怎么办?不适用于较新的版本。最近的答案是:。这些评论很有趣。