问题很简单,但答案似乎很难得到。在Codeigniter中,我可以加载URL助手,然后简单地执行
echo base_url();
获取我网站的URL。在Laravel中也有类似的情况吗?
问题很简单,但答案似乎很难得到。在Codeigniter中,我可以加载URL助手,然后简单地执行
echo base_url();
获取我网站的URL。在Laravel中也有类似的情况吗?
当前回答
这样的:
echo url('/');
这:
echo asset('/');
在我的情况下,两者都显示了主页url:)
其他回答
我用了这个,它在Laravel 5.3.18中为我工作:
<?php echo URL::to('resources/assets/css/yourcssfile.css') ?>
重要提示:这只会在你已经从你的URL中删除“公共”时起作用。要做到这一点,您可以查看这个有用的教程。
要获得应用程序的url,你可以使用:
Config::get('app.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') }}">
我找到了另一种方法来获取基本url来显示环境变量APP_URL的值
env('APP_URL')
它将显示类似http://domains_your//yours_website的基本url。注意,它假定您已经在.env文件(存在于根文件夹中)中设置了环境变量。
顺便说一下,如果你的路线有一个这样的名字:
Route::match(['get', 'post'], 'specialWay/edit', 'SpecialwayController@edit')->name('admin.spway.edit');
你可以像这样使用route()函数:
<form method="post" action="{{route('admin.spway.edit')}}" class="form form-horizontal" id="form-spway-edit">
其他有用功能:
$uri = $request->path();
$url = $request->url();
$url = $request->fullUrl();
asset()
app_path();
// ...
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/helpers.php