将图像链接到 Laravel 中的路线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14832753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linking images to routes in Laravel
提问by Khalid Khalil
I'm working on laravel php framework , I've a simple question , While using blade template engine , I normally use html helper functions to render forms , links or whatever so .
我正在研究 laravel php 框架,我有一个简单的问题,在使用刀片模板引擎时,我通常使用 html 辅助函数来呈现表单、链接或其他任何内容。
Now when i try to put a link to specific route , i usually to like that :
现在,当我尝试添加指向特定路线的链接时,我通常会喜欢:
{{ HTML::link_to_route('author','TheAuthor')) }}
First parameter takes the route , and the second one takes the string that will appear , To make it simpler that code will produce :
第一个参数采用路线,第二个参数采用将出现的字符串,为了简化代码将产生的内容:
<a href="ROUTE URL">TheAuthor</a>
Now I want to replace TheAuthor with an image , What can i do ?
现在我想用图片替换 TheAuthor,我该怎么办?
回答by Gafitescu Daniel
Or you can use {{route}}
或者你可以使用 {{route}}
Ex:
前任:
<a href="{{route($route,$params)}}"><img src="'.$image.'" /></a>
回答by Laurence
I know this is not the answer you want to hear - but you cannot pass any image via link_to_route.
我知道这不是您想听到的答案 - 但您无法通过 link_to_route 传递任何图像。
The problem is the output from the HTML class is escaped automatically. So if you try to pass this:
问题是 HTML 类的输出会自动转义。所以如果你试图通过这个:
{{ HTML::link_to_route('author','<img src="'.URL::base().'assets/images/image.jpg" alt="icon" />')) }}
it comes out like this:
它是这样出来的:
<img src="http://laravel3.dev/assets/images/image.jpg" alt="icon" />
which will just be text on the screen - no image. Instead you need to use URI::to_route('author')
and generate the link yourself. So make a helper a like this (not tested):
这将只是屏幕上的文本 - 没有图像。相反,您需要自己使用URI::to_route('author')
和生成链接。所以做一个这样的助手(未测试):
function link_to_route_image($route, $image)
{
$m = '<a href="'.URL::to_route($route).'">'
. '<img>'.$image.'</img>'
. '</a>';
return $m;
}
回答by Tim
It may become tedious if you are doing this all over the place, but in isolated spots the best way to handle this would actually be to wrap your entire link_to_* method call in HTML::decode.
如果你到处这样做可能会变得乏味,但在孤立的地方,处理这个问题的最佳方法实际上是将整个 link_to_* 方法调用包装在 HTML::decode 中。
{{ HTML::decode(HTML::link_to_route('author','<img src="{URL::base()}assets/images/image.jpg" alt="icon" />')) }}
There's nothing you can do from the inside of the function (such as wrapping the "title" portion in decode) because the cleaning is happening within the function call, but if you decode the entire link it will render the html properly.
您无法从函数内部执行任何操作(例如在解码中包装“标题”部分),因为清理发生在函数调用中,但是如果您解码整个链接,它将正确呈现 html。