0

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'));

    }
A.L
  • 10,259
  • 10
  • 67
  • 98
theo theo
  • 131
  • 1
  • 3
  • 10

1 Answers1

1

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
ferdynator
  • 6,245
  • 3
  • 27
  • 56
  • yes but the picture I will load them also from my database, It means this solution won't help – theo theo Oct 20 '13 at 22:25
  • +1 this is good way but mine is not that bad although I know its not fit with mvc standard but it would give the answer .please check my updated answer. – Sina R. Oct 20 '13 at 22:59
  • @theotheo Check my answer again. You probably forgot to install your assets. – ferdynator Oct 21 '13 at 19:38
  • Why is it so complicated in Symfony to show a picture dynamically, your idea is not bad But I'm searching for something like functions etc, bc with your wy it's a bit complicated bc after my picture I have also for example to send comments and and ... for the moment i'm waiting only for a simple functions that resolve that – theo theo Oct 21 '13 at 19:48