在Laravel v4中,我可以使用…

Route::currentRouteName()

我如何在Laravel v5和Laravel v6中做到这一点?


当前回答

您要做的第一件事是在类的顶部导入命名空间。

use Illuminate\Support\Facades\Route;

拉拉维尔V8

$route = Route::current(); // Illuminate\Routing\Route
$name = Route::currentRouteName(); // RouteName
$action = Route::currentRouteAction(); // Action

Laravel v7、6和5.8

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

其他回答

查看\Illuminate\Routing\Router.php,您可以通过在控制器方法中注入路由器来使用方法currentRouteNamed()。例如:

use Illuminate\Routing\Router;
public function index(Request $request, Router $router) {
   return view($router->currentRouteNamed('foo') ? 'view1' : 'view2');
}

或者使用Route facade:

public function index(Request $request) {
   return view(\Route::currentRouteNamed('foo') ? 'view1' : 'view2');
}

你也可以使用is()方法来检查路由是否被命名为任何给定的参数,但要注意这个方法使用preg_match(),我曾经经历过它导致使用虚线路由名的奇怪行为(如'foo.bar.done')。还有preg_match()的性能问题 这是PHP社区的一个大主题。

public function index(Request $request) {
    return view(\Route::is('foo', 'bar') ? 'view1' : 'view2');
}

在laravel 7或8使用辅助功能

获取当前路由名

request()->route()->getName()

要检查路由是否是当前最好创建自己的方法为请求类使用宏

在AppServiceProvider中启动方法:

use Illuminate\Support\Facades\Request;
public function boot()
    {
        Request::macro('isCurrentRoute', function ($routeNames) {
            $bool = false;
            foreach (is_array($routeNames) ? $routeNames : explode(",",$routeNames) as $name) {
               if(request()->routeIs($name)) {
                   $bool = true;
                   break;
                }
             }

             return $bool;
        });
    }

您可以在刀片或控制器中使用此方法

request()->isCurrentRoute('foo') // string route
request()->isCurrentRoute(['bar','foo','xyz.*']) //array routes
request()->isCurrentRoute('blogs,foo,bar,xyz.*') //string route seperated by comma

您可以使用内置laravel路由方法

request()->routeIs('home');
request()->routeIs('blogs.*'); //using wildcard

由于某些原因,我不能使用这些解决方案。所以我刚刚在web.php中声明了我的路由$router->get('/api/v1/users', ['as' => 'index', 'uses' => 'UserController@index']),在我的控制器中,我得到了路由的名称使用$routeName = $request->route()[1]['as'];在UserController的索引方法中,哪个$request是\Illuminate\Http\ request $request类型暗示参数

使用Lumen 5.6。希望它能帮助到一些人。

找到了一种查找当前路由名称的方法,适用于laravel v5, v5.1.28和v5.2.10

名称空间

use Illuminate\Support\Facades\Route;

and

$currentPath= Route::getFacadeRoot()->current()->uri();

对于Laravel Laravel v5.3,你可以使用:

use Illuminate\Support\Facades\Route;

Route::currentRouteName();

试试这个

Route::getCurrentRoute()->getPath();

or

\Request::route()->getName()

从v5.1

use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();

与 Laravel v5.2 相关:

Route::currentRouteName(); //use Illuminate\Support\Facades\Route;

或者如果你需要动作名

Route::getCurrentRoute()->getActionName();

Laravel 5.2路由文档

获取请求URI

path方法返回请求的URI。因此,如果传入请求的目标是http://example.com/foo/bar, path方法将返回foo/bar:

$uri = $request->path();

is方法允许您验证传入的请求URI是否与给定的模式匹配。在使用此方法时,您可以使用*字符作为通配符:

if ($request->is('admin/*')) {
    //
}

要获得完整的URL,而不仅仅是路径信息,你可以在请求实例上使用URL方法:

$url = $request->url();

Laravel v5.3…v5.8

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

Laravel 5.3路由文档

Laravel v6 . x 7. x

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

**截至2019年11月11日- 6.5版**

Laravel 6。X路由文档

有一个选项可以使用request来获取路由

$request->route()->getName();