在 Laravel 5.7 中获取当前 URI

Far*_*han 5 laravel laravel-5.7

我知道在 Laravel 的早期版本(例如 4)中,可以通过以下方式获取当前 uri

Route::current()->uri();
Run Code Online (Sandbox Code Playgroud)

然而,这在 Laravel 5.7 或更高版本中似乎不起作用。我想知道应该如何重写。请注意,我正在刀片视图中访问 uri,因此无法使用非静态方法。

joy*_*joy 10

您可以使用以下方法获取 Laravel 中的当前 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();
Run Code Online (Sandbox Code Playgroud)

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

use Illuminate\Support\Facades\URL;

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

有关详细信息,您可以在此处阅读完整文档。


Dwi*_*ght 5

这应该仍然有效 - 但如果当前路径未命名,它可能无法按预期工作。您可能应该从请求中获取路径。

Request::path();
Run Code Online (Sandbox Code Playgroud)

它可能还会检查请求实例的 API,因为您可以调用许多相关方法。

Request::root();
Request::url();
Request::fullUrl();
Request::fullUrlWithQuery();
Run Code Online (Sandbox Code Playgroud)