您可以使用 URL 外观,它可以让您调用 URL generator
所以你可以这样做:
URL::to('/');
您还可以使用应用程序容器:
$app->make('url')->to('/');
$app['url']->to('/');
App::make('url')->to('/');
或者注入 UrlGenerator:
<?php
namespace Vendor\Your\Class\Namespace;
use Illuminate\Routing\UrlGenerator;
class Classname
{
protected $url;
public function __construct(UrlGenerator $url)
{
$this->url = $url;
}
public function methodName()
{
$this->url->to('/');
}
}
拉拉维尔 < 5.2
echo url();
拉拉维尔> = 5.2
echo url('/');
url('/')
asset('/')
对我来说似乎更好,因为它有斜杠,这在基本 href 中是必需的。
对于 Laravel 5,我通常使用:
<a href="{{ url('/path/uri') }}">Link Text</a>
我的理解是使用 url()
函数调用与 URL::to()
相同的 Facade
secure_url()
函数,这将产生一个 https 链接。在 https 站点上使用 url()
仍会产生一个 http 链接。
url()
会根据请求的协议生成 http 或 https 链接。另一方面 secure_url()
不存在。 Laravel 5.4 也有这种变化吗?
secure_url()
的 5.4 Laravel 文档:laravel.com/docs/5.4/helpers#method-secure-url
2018 Laravel release(5.7) 文档的更新,增加了一些 url() 函数及其用法。
问题:在 Laravel 中获取站点的 URL?这是一个普遍的问题,所以我们可以拆分它。
1. 访问基本 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. URLs To Assets(Public)
// 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 外观访问:
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 一起工作,我必须这样做:
asset('/');
<base href="{{ asset('/') }}" />
<base href="..."/>
这个:
echo url('/');
和这个:
echo asset('/');
在我的情况下,两者都显示了主页 url :)
Laravel 提供了一堆辅助函数,根据您的要求,您可以简单地
使用 Laravel Helpers 的 url() 函数
但在 Laravel 5.2 的情况下,您将不得不使用 url('/')
here 是 Laravel 的所有其他辅助函数的列表
url('/css/style.css')
要获取您配置的应用程序 url,您可以使用:
Config::get('app.url')
app.url
它似乎是一个后备
检查这个 -
<a href="{{url('/abc/xyz')}}">Go</a>
这对我有用,我希望它对你有用。
另一种可能性:{{ URL::route('index') }}
有多种方式:
请求()->getSchemeAndHttpHost()
网址('/')
资产('')
$_SERVER['SERVER_NAME']
你也可以使用 URL::to('/') 在 Laravel 中显示图像。请看下面:
<img src="{{URL::to('/')}}/images/{{ $post->image }}" height="100" weight="100">
假设您的图像存储在“public/images”下。
我使用了它,它在 Laravel 5.3.18 中对我有用:
<?php echo URL::to('resources/assets/css/yourcssfile.css') ?>
重要提示:这仅在您已从 URL 中删除“公共”时才有效。为此,您可以查看 this helpful tutorial。
顺便说一句,如果您的路线名称如下:
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
您可以按照以下方式使用外观或辅助功能。
echo URL::to('/');
echo url();
Laravel 使用 Symfony 组件进行请求,Laravel 内部逻辑如下。
namespace Symfony\Component\HttpFoundation;
/**
* {@inheritdoc}
*/
protected function prepareBaseUrl()
{
$baseUrl = $this->server->get('SCRIPT_NAME');
if (false === strpos($this->server->get('REQUEST_URI'), $baseUrl)) {
// assume mod_rewrite
return rtrim(dirname($baseUrl), '/\\');
}
return $baseUrl;
}
我找到了另一种方法来获取基本 url 以显示环境变量 APP_URL 的值
env('APP_URL')
这将显示像 http://domains_your//yours_website 这样的基本 URL。请注意,它假定您已在 .env 文件(位于根文件夹中)中设置了环境变量。
你可以从请求中得到它,在 laravel 5
request()->getSchemeAndHttpHost();
我还使用了 base_path()
函数,它对我有用。
不定期副业成功案例分享
/something/
中,它将生成正确的 URL。{{URL::to('/my-page.html')}}
并查看代码echo URL::to('/my-page.html');
<a class="button is-primary" href="<?= URL::to('/'); ?>/atencion/reporte" target="_blank">
,谢谢!