Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Get external content when rendering template

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 :) ?

like image 970
Pierre de LESPINAY Avatar asked Jan 27 '26 07:01

Pierre de LESPINAY


1 Answers

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>
like image 120
a.aitboudad Avatar answered Jan 28 '26 23:01

a.aitboudad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!