在Laravel 5.7中获取当前URL

问题描述 投票:0回答:3

我知道在以前版本的Laravel(例如4)中,人们可以获得当前的uri

Route::current()->uri();

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

laravel laravel-5.7
3个回答
1
投票

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

Request::path();

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

Request::root();
Request::url();
Request::fullUrl();
Request::fullUrlWithQuery();

2
投票

您可以使用以下方法获取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();

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

use Illuminate\Support\Facades\URL;

echo URL::current();

有关更多信息,您可以阅读完整的文档here


0
投票
public function yourMethod(Request $request)
{
    return view('your-view', [ 'current-uri' => $request->route()->uri() ]);
}

Documentation

© www.soinside.com 2019 - 2024. All rights reserved.