问题很简单,但答案似乎很难得到。在Codeigniter中,我可以加载URL助手,然后简单地执行

echo base_url();

获取我网站的URL。在Laravel中也有类似的情况吗?


当前回答

看看这个——

<a href="{{url('/abc/xyz')}}">Go</a>

这对我很有用,我希望对你也有用。

其他回答

为了让它与不漂亮的url一起工作,我必须做:

asset('/');

Laravel < 5.2

echo url();

Laravel >= 5.2

echo url('/');

2018年Laravel发行版(5.7)文档的更新,增加了更多url()函数及其用法。

问:在Laravel中获取该网站的URL ? 这是一个普遍的问题,所以我们可以把它分开。

1. 访问Base URL

// Get the base URL.
echo url('');

// Get the app URL from configuration which we set in .env file.
echo config('app.url'); 

2. 访问当前URL

// Get the current URL without the query string.
echo url()->current();

// Get the current URL including the query string.
echo url()->full();

// Get the full URL for the previous request.
echo url()->previous();

3.命名路由的url

// http://example.com/home
echo route('home');

4. 资产的url(公共)

// Get the URL to the assets, mostly the base url itself.
echo asset('');

5. 文件的url

use Illuminate\Support\Facades\Storage;

$url = Storage::url('file.jpg'); // stored in /storage/app/public
echo url($url);

这些方法也可以通过URL facade访问:

use Illuminate\Support\Facades\URL;

echo URL::to(''); // Base URL
echo URL::current(); // Current URL

如何使用刀片模板(视图)调用这些Helper函数。

// http://example.com/login
{{ url('/login') }}

// http://example.com/css/app.css
{{ asset('css/app.css') }}

// http://example.com/login
{{ route('login') }}

// usage

<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">

<!-- Login link -->
<a class="nav-link" href="{{ route('login') }}">Login</a>

<!-- Login Post URL -->
<form method="POST" action="{{ url('/login') }}">

这样的:

echo url('/');

这:

echo asset('/');

在我的情况下,两者都显示了主页url:)

有多种方法:

请求()- > getSchemeAndHttpHost () url(“/”) 资产(“) 配置(“app.url”) $ _SERVER(“SERVER_NAME”)