在Laravel v4中,我可以使用…

Route::currentRouteName()

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


当前回答

它不需要记忆任何东西,当你想请求某个变量时,通过dd(Request())可以评估哪个变量对你来说是突出的。 在下图中,它很清楚。

如果你想获取路径,显然,这段代码会显示

dd(request()->getpathInfo())

别忘了嵌入 使用说明\ Http \请求; 例如:

if(request()->getpathInfo()=="/logadmin"){
do somethings....
}

其他回答

在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。希望它能帮助到一些人。

如果你想在多条路线上选择菜单,你可以这样做:

<li class="{{ (Request::is('products/*') || Request::is('products') || Request::is('product/*') ? 'active' : '') }}"><a href="{{url('products')}}"><i class="fa fa-code-fork"></i>&nbsp; Products</a></li>

或者如果你想只选择一个菜单,你可以简单地这样做:

<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i>&nbsp; Users</a></li>

在Laravel 5.2中也进行了测试

希望这能帮助到一些人。

使用帮助:

app('request')->route()->getName()

在控制器中访问当前路由名

Ie - http://localhost/your_project_name/edit

$request->segment(1);  // edit

(或)

$request->url();  // http://localhost/your_project_name/edit