1

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(i.e. 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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

i know about route forwarding. But what i want to know is which is the correct method?? . And is making curl request internally the correct approach
If you need to resort to Curl to call a method in your application, it means you have serious architectural problems in your code. You should outsource whatever business logic you have in your controller to services. Controllers should only deal with http , not how datas are managed.
i do not need to resort to curl. i just wanted to know is it the correct approach and from your answer i think it is NOT! So should i opt for route forwarding?
In symfony2 perspective url forwarding is the best method, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.