简体   繁体   English

Laravel外部URL问题

[英]Laravel external URL issue

I want to set external URL in some of my anchor tags but facing problem when URL is without http/https protocol. 我想在一些锚标记中设置外部URL,但是当URL没有http / https协议时会遇到问题。 When URL is without these protocols URL become like following: 当URL没有这些协议时,URL如下所示:

<a target="_blank" href="www.usatoday.com/"></a>

when I click on it or hover over it, it shows and redirect me to: http://localhost/event/www.usatoday.com 当我单击它或将鼠标悬停在它上面时,它显示并将我重定向到: http://localhost/event/www.usatoday.com

I've tried following two ways in href but didn't worked: 我尝试了以下两种方法在href中工作,但没有成功:

{{url($eventSponsorsRow->sponsorUrl)}}
{{ $eventSponsorsRow->sponsorUrl }}

instead of URL in href. 而不是href中的URL。 Laravel 5.3 Laravel 5.3

This is HTML and browser stuff, not Laravel/PHP itself. 这是HTML和浏览器内容,而不是Laravel / PHP本身。

You just need to provide protocol part of the url to make it external. 您只需要提供URL的协议部分以使其成为外部即可。

You can skip the http: but it needs at least double slash in front of the url like: 您可以跳过http:但它至少需要在网址前面加双斜杠,例如:

<a target="_blank" href="//www.usatoday.com/"></a>

Please not, that if you skip the http(s): part it will use currently used protocol. 请不要,如果您跳过http(s):部分,它将使用当前使用的协议。

You need to prepend http:// to the url 您需要在网址前面添加http://

if you want to use {{url($eventSponsorsRow->sponsorUrl)}} make sure when you insert into a database it prepends http:// with it 如果您想使用{{url($ eventSponsorsRow-> sponsorUrl)}},请确保当您插入数据库时​​,它会以http:// 开头

<a target="_blank" href="{{ url($eventSponsorsRow->sponsorUrl) }}"></a>

if you don't want to do that you will need to create a variable with the url you are getting from the database and prepend the http:// 如果您不想这样做,则需要使用从数据库获取的url创建一个变量,并在http://前面加上

$newvariable = "http://$eventSponsorsRow->sponsorUrl"; and call the $newvariable in the correct location 并在正确的位置调用$ newvariable

<a target="_blank" href="{{ $newvariable }}"></a>

https://laracasts.com/discuss/channels/laravel/laravel-blade-external-url-link-reading-url-from-database https://laracasts.com/discuss/channels/laravel/laravel-blade-external-url-link-reading-url-from-database

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM