I want to have some picture displayed when I click on some links but I don't know how to send them from the controller because everytime I have as src= {{ asset('bundles/fds/...') }}.How can I solve it?
public function getPictureAction() {
$html = '<div id="comment_list_div">';
$html .= '<div id="comment_list_div_img">';
$html .= '<div id="comment_list_div_im">';
$html .= '<a href=""><img src="{{ asset('bundles/fds/images/Picture.png') }}"/></a>';
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
$return = array( "html" => $html);
$return = json_encode($return);
return new Response($return, 200, array('Content-Type'=>'application/json'));
}
The correct way to do this would be to move your html
code into a template and render it in your action:
In your controller:
use Symfony\Component\HttpFoundation\JsonResponse;
// ...
public function getPictureAction() {
// load your picture from the db
$content = $this->renderView(
'AcmeHelloBundle:Json:JsonResponse.html.twig',
// pass the picture to your template:
array('imagePath' => $image->getPath())
);
return new JsonResponse(
$content,
200,
array('Content-Type'=>'application/json')
);
}
And your template:
<div id="comment_list_div">
<div id="comment_list_div_img">
<div id="comment_list_div_im">
{# use the vairable name you passed to this template to acces your image #}
<a href=""><img src="{{ asset(imagePath) }}"/></a>
</div>
</div>
</div>
Also make sure your assets are in place:
php app/console assets:install
php app/console assetic:dump --env=dev