简体   繁体   中英

API driven symfony2 project

Im trying to create an API driven project in symfony2 using FOSRestBundle .

I have an APIController with an action getDataAction().

This works perfectly when i send requests from some external application.I get a JSON encoded data as expected.

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. OR
  • or sending a CURL request from the other controller to getDataAction() of the APIController to get the data.

You can get data from an APIController by using route forwarding . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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