Link with icon in Laravel 4

2020-06-16 04:41发布

问题:

Can someone help to rewrite this, from HTML to Laravel4?

    <a href="index.php" ><span><i class="icon-home"></i></span> Home </a>

The route name for that page is just '/'. I know how to write simple link in Laravel:

{{ HTML::link('/','Home) }}

But how can I add the span class with the font-awesome icon?

回答1:

I'd just put the link in the href.

<a href="{{ url('/') }}"><span><i class="icon-home"></i></span> Home</a>

No need to generate all the rest through Laravel.



回答2:

What @Dries suggests is simple and very straightforward, but you really want to have it done entirely via Laravel, I would suggest writing a HTML macro, especially if you want more complex html structures to be involved. For example, here is a macro for <a><img /></a> structure:

    HTML::macro('image_link', function($url = '', $img='img/', $alt='', $param = false, $active=true, $ssl=false)
{
    $url = $ssl==true ? URL::to_secure($url) : URL::to($url);  
    $img = HTML::image($img,$alt);
    $link = $active==true ? HTML::link($url, '#', $param) : $img;
    $link = str_replace('#',$img,$link);
    return $link;
}); 

You could read more about it here: http://forums.laravel.io/viewtopic.php?pid=10467



回答3:

{!! HTML::decode(link_to(URL::previous(),
    '<i class="fa fa-chevron-left" aria-hidden="true"></i> Back',
    ['class' => 'btn btn-primary'])) !!}