简体   繁体   English

API驱动的symfony2项目

[英]API driven symfony2 project

Im trying to create an API driven project in symfony2 using FOSRestBundle . 我试图使用FOSRestBundle在symfony2中创建一个API驱动的项目。

I have an APIController with an action getDataAction(). 我有一个APIController getDataAction().

This works perfectly when i send requests from some external application.I get a JSON encoded data as expected. 当我从某个外部应用程序发送请求时,这非常有效。我按预期获得了JSON编码的数据。

But i'll be needing that data in the same application as well(ie some other controller within the same application). 但是我也需要在同一应用程序中使用该数据(即同一应用程序中的某些其他控制器)。

Which is an appropriate method of doing this? 哪种方法合适?

  • creating object of the APIController and then calling required action on it. 创建APIController对象,然后对其调用所需的操作。 OR 要么
  • or sending a CURL request from the other controller to getDataAction() of the APIController to get the data. 或发送来自其它控制器到CURL请求getDataAction()所述的APIController来获取数据。

You can get data from an APIController by using route forwarding . 您可以使用路由转发从APIController获取数据。 By using this you can the use any action in your controller. 通过使用此功能,您可以在控制器中使用任何操作。 No need for the curl or separate controller object. 无需卷曲或单独的控制器对象。 some thing like this : 像这样的东西:

 $details = $this->forward('acmeRestBundle:API:getData',array('_format'=>'json'));

That will return the result-set from that action.May be like this : 这将返回该操作的结果集。可能像这样:

  content":protected]=>
  string(107181) "{}"
  ["version":protected]=>
  string(3) "1.0"
  ["statusCode":protected]=>
  int(200)
  ["statusText":protected]=>
  string(2) "OK"
  ["charset":protected]=>
  NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM