php Laravel:获取基本网址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23059918/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:27:45  来源:igfitidea点击:

Laravel: Get base url

phplaravelbase-url

提问by Inigo

Simple question, but the answer seems quite hard to come by. In Codeigniter, I could load the url helper and then simply do

一个简单的问题,但答案似乎很难得到。在 Codeigniter 中,我可以加载 url helper 然后简单地做

echo base_url();

to get my site's URL. Is there an equivalent in Laravel?

获取我网站的网址。Laravel 中是否有等价物?

回答by hannesvdvreken

You can use the URL facade which lets you do calls to the URL generator

您可以使用 URL 外观,它可以让您调用URL 生成器

So you can do:

所以你可以这样做:

URL::to('/');

You can also use the application container:

您还可以使用应用程序容器:

$app->make('url')->to('/');
$app['url']->to('/');
App::make('url')->to('/');

Or inject the UrlGenerator:

或者注入 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('/');
    }
}

回答by Nguy?n Thành B?i

Laravel < 5.2

Laravel < 5.2

echo url();

Laravel >= 5.2

Laravel >= 5.2

echo url('/');

Hope this helps you

希望这对你有帮助

回答by DrewT

For Laravel 5 I normally use:

对于 Laravel 5,我通常使用:

<a href="{{ url('/path/uri') }}">Link Text</a>

I'm of the understanding that using the url()function is calling the same Facadeas URL::to()

我的理解是,使用url()函数调用是相同FacadeURL::to()

回答by Sobin Augustine

Updates from 2018 Laravel release(5.7) documentation with some more url() functions and it's usage.

从 2018 Laravel release(5.7) 文档更新了一些 url() 函数及其用法。

Question: To get the site's URL in Laravel?

This is kind of a general question, so we can split it.

问题:要在 Laravel 中获取站点的 URL?

这是一个普遍的问题,所以我们可以拆分它。

1. Accessing The Base 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. Accessing The Current 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. URLs For Named Routes

3. 命名路由的 URL

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

4. URLs To Assets(Public)

4. 资产的 URLs(Public)

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

5. File URLs

5. 文件 URL

use Illuminate\Support\Facades\Storage;

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

Each of these methods may also be accessed via the URL facade:

这些方法中的每一个也可以通过 URL 门面访问:

use Illuminate\Support\Facades\URL;

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

How to call these Helper functions from blade Template(Views) with usage.

如何从刀片模板(视图)中调用这些辅助函数和用法。

// 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') }}">

回答by dan-klasson

To get it to work with non-pretty URLs I had to do:

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

asset('/');

回答by Akshay Khale

Laravel provides bunch of helper functions and for your requirement you can simply

Laravel 提供了一堆辅助函数,根据您的要求,您可以简单地

use url()function of Laravel Helpers

使用Laravel Helpers 的url()函数

but in case of Laravel 5.2you will have to use url('/')

但在Laravel 5.2 的情况下,你将不得不使用url('/')

hereis the list of all other helper functions of Laravel

是 Laravel 的所有其他辅助函数的列表

回答by GGadash

This:

这个:

echo url('/');

And this:

和这个:

echo asset('/');

both displayed the home url in my case :)

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

回答by CptChaos

Another possibility: {{ URL::route('index') }}

另一种可能: {{ URL::route('index') }}

回答by Kenyon

To just get the app url, that you configured you can use :

要获取您配置的应用程序网址,您可以使用:

Config::get('app.url')

回答by Niladri Banerjee - Uttarpara

You can also use URL::to('/') to display image in Laravel. Please see below:

您还可以使用 URL::to('/') 在 Laravel 中显示图像。请参阅以下内容:

<img src="{{URL::to('/')}}/images/{{ $post->image }}" height="100" weight="100"> 

Assume that, your image is stored under "public/images".

假设您的图像存储在“public/images”下。