如何获取完整的 URL 以在 Laravel 中显示

Ano*_*box 3 php url http request laravel

我是 laravel 的初学者,我想在页面上显示当前页面的实时 URL。

// 完整 URL,带有查询字符串 $request->fullUrl()

// 只是 URL 的路径部分 $request->path()

// 只是 URL 的根(协议和域)部分) $request->r强调文本oot()

在谷歌上搜索后我发现了这一点,任何人都可以告诉我这是否是正确的方法以及如何实现这一点。比如在“路由”中写什么以及在“控制器”中写什么。

ste*_*tef 7

来自文档

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

// Get the current URL including the query string...
echo url()->full();
Run Code Online (Sandbox Code Playgroud)

这些方法中的每一个都可以通过 URL 外观来访问:

use Illuminate\Support\Facades\URL;

echo URL::current();
Run Code Online (Sandbox Code Playgroud)

如果您使用命名路由(不是强制性的):

echo route('post.show', ['post' => 1]);

// http://example.com/post/1
Run Code Online (Sandbox Code Playgroud)