ChatGPT解决这个技术问题 Extra ChatGPT

Laravel:获取基本 URL

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

echo base_url();

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


h
hannesvdvreken

您可以使用 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('/');
    }
}

尽管它可能在示例中出现,但它与 Laravel 的根路径相关,因此如果您安装在 /something/ 中,它将生成正确的 URL。
@deFreitas 和 #ceejayoz 如何在 Laravel 本地化中使用 URL::to?
@MubasharIqbal 如果我理解您的问题,请查看 {{URL::to('/my-page.html')}} 并查看代码 echo URL::to('/my-page.html');
不错的处理程序,就我而言:<a class="button is-primary" href="<?= URL::to('/'); ?>/atencion/reporte" target="_blank">,谢谢!
A
Anye

拉拉维尔 < 5.2

echo url();

拉拉维尔> = 5.2

echo url('/');

您能否扩展此答案以包含解释而不仅仅是代码片段?
考试:我在本地有站点:localhost/abc 在 Codeigniter 中:echo base_url(); =>我在 Laravel 中得到 localhost/abc:echo url(); =>我也得到localhost/abc
将此用于带有分段的 url:url().'/'.\Request::segment(1).'/'.\Request::segment(2)
请注意,这在 5.2 中不再有效:github.com/laravel/framework/issues/11479 但是您可以使用 url('/')
asset('/') 对我来说似乎更好,因为它有斜杠,这在基本 href 中是必需的。
D
DrewT

对于 Laravel 5,我通常使用:

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

我的理解是使用 url() 函数调用与 URL::to() 相同的 Facade


注意:如果您的网站是通过 https 提供的,您可以以同样的方式使用 secure_url() 函数,这将产生一个 https 链接。在 https 站点上使用 url() 仍会产生一个 http 链接。
这个对我有用。了解secure_url() 语法也很有用。谢谢。
我正在使用流明 5.4。 url() 会根据请求的协议生成 http 或 https 链接。另一方面 secure_url() 不存在。 Laravel 5.4 也有这种变化吗?
不,它是 Larvel 5.4 的一部分。我无法真正发表评论,因为我从未使用过 Lumen,但可在此处获得 secure_url() 的 5.4 Laravel 文档:laravel.com/docs/5.4/helpers#method-secure-url
C
Community

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

d
dan-klasson

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

asset('/');

根据我的需要,这是最好的<base href="{{ asset('/') }}" />
向你致敬!由于尾部斜杠,它正好适合 <base href="..."/>
j
joshuamabina

这个:

echo url('/');

和这个:

echo asset('/');

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


A
Akshay Khale

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

使用 Laravel Helpers 的 url() 函数

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

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


@sambellerose,您是否想访问可以执行的内部文件夹/文件url('/css/style.css')
M
Mostafa Norzade

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

Config::get('app.url')

此定义仅在 Laravel cli 中使用。 app.url 它似乎是一个后备
好吧, env('APP_URL') 可能是最好的选择。
v
vikash singh

检查这个 -

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

这对我有用,我希望它对你有用。


C
CptChaos

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


不知道为什么这被否决了,因为它确实有效,而且没有人提供选择?
不是我的反对意见,但我猜原因是您不能保证根路由的命名在每个应用程序中都是“索引”。
X
X 47 48 - IR

有多种方式:

请求()->getSchemeAndHttpHost()

网址('/')

资产('')

$_SERVER['SERVER_NAME']


N
Niladri Banerjee - Uttarpara

你也可以使用 URL::to('/') 在 Laravel 中显示图像。请看下面:

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

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


I
ITWitch

我使用了它,它在 Laravel 5.3.18 中对我有用:

<?php echo URL::to('resources/assets/css/yourcssfile.css') ?>

重要提示:这仅在您已从 URL 中删除“公共”时才有效。为此,您可以查看 this helpful tutorial


E
Ethan

顺便说一句,如果您的路线名称如下:

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


P
Paresh Barad

您可以按照以下方式使用外观或辅助功能。

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;
}

A
Adeel Raza Azeemi

我找到了另一种方法来获取基本 url 以显示环境变量 APP_URL 的值

env('APP_URL')

这将显示像 http://domains_your//yours_website 这样的基本 URL。请注意,它假定您已在 .env 文件(位于根文件夹中)中设置了环境变量。


S
Soleil

你可以从请求中得到它,在 laravel 5

request()->getSchemeAndHttpHost();

M
Mostafa Norzade

我还使用了 base_path() 函数,它对我有用。