3

Beginner in Symfony2, so maybe it's a dumb question.

I would need to get the response of an HTTP query (external server) and put it on a template before sending it to the client.

Like

<div id="main_content">
  Lorem Ipsum
  <div id="external_content">
    {% get_content_by_url 'http://external.com/uri' params_object %}
  </div>
</div>

Or maybe I should get the response from the controller and pass it as a variable to the template ?

What is the best practice (or am I on a totaly wrong way :) ?

2
  • Hi, there are different ways to do: 1. Get the content in the controller and render it in twig 2. Let twig call an extra controller which handles the data 3. include the data with jQuery (ajax) Commented May 23, 2012 at 7:35
  • 1 and 2 seem better for security Commented May 23, 2012 at 7:57

1 Answer 1

8

you can use this bundle

after enters this code in your controller:

$crawler = $client->request('GET', 'http://symfony-reloaded.org/');
$response = $client->getResponse();
$content = $response->getContent();

and finaly in file twig :

<div id="main_content">
    Lorem Ipsum
<div id="external_content">
    {{ content  }}
</div>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

This seems simple. Can we do a POST request with parameters with the same method ?
yes : '$crawler = $client->request('POST', 'symfony-project.org/',params request); '

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.