Linking an Image Using the HTML Helper

I am trying to create a link to an image using the Laravel 4 HTML helper. But this doesn't seem to work. I have this line of code

{{ HTML::link("#", HTML::image("img/logo.png", "Logo") ) }} 

But this simply displays strin as follows:

 <img src="http://localhost/worker/public/img/logo" alt="Logo"> 

How come. ??

+6
source share
6 answers

You will probably have to:

 <a href="#">{{ HTML::image("img/logo.png", "Logo") }}</a> 

Because link () uses entities to exit the header:

 public function link($url, $title = null, $attributes = array(), $secure = null) { $url = $this->url->to($url, array(), $secure); if (is_null($title) or $title === false) $title = $url; return '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>'; } 

Creating this source code:

 "<a href="#">&lt;img src=&quot;http://localhost/img/logo.png&quot; alt=&quot;Logo&quot;&gt;</a>" 
+13
source

I think this is unnecessary for no reason. I would do:

 <a href="#"><img src={{asset('img/logo.png')}} alt="Logo"></a> 

If I need a dynamic link instead of #, I would do:

 <a href="{{URL::to('/')}}"><img src={{asset('img/logo.png')}} alt="Logo"></a> 

Try using html as much as possible.

+33
source

You can also use html_entity_decode to make your code work

 {{ html_entity_decode( HTML::link("#", HTML::image("img/logo.png", "Logo") ) ) }} 
+7
source

Additionally, for the correct answer, you can also add some attribute, for example, height and width

 <a href="#">{{ HTML::image("img/logo.png", "Logo",array('height'=>'100','width'=>'100')) }}</a> 
0
source
 \Html::tag('a', \Html::image('image path')->toHtml(), ['href' => 'link']); 
0
source

HTML Helper removed from Laravel 5

now you can just use this

 <img src="{{ asset('logo.png') }}" alt="logo"> 

This asset defaults to the shared folder of your larval application

0
source

Source: https://habr.com/ru/post/954609/


All Articles