Laravel:在视图(view)中显示控制器(controller), Get Laravel controller name in view

Laravel:在视图(view)中显示控制器(controller), Get Laravel controller name in view
Laravel:在视图(view)中显示控制器(controller), Get Laravel controller name in view

 

如果您的布局是 Blade 模板,您可以创建一个视图合成器,将这些变量注入到您的布局中。在app/Providers/AppServiceProvider.php添加如下内容:

public function boot()
{
    app('view')->composer('layouts.master', function ($view) {
        $action = app('request')->route()->getAction();

        $controller = class_basename($action['controller']);

        list($controller, $action) = explode('@', $controller);

        $view->with(compact('controller', 'action'));
    });
}

然后,您的布局模板中将有两个可用变量:$controller$action.

 

或者:

I use a simple solution. You can test and use it in everywhere, also in your views:

{{ dd(request()->route()->getAction()) }}

 

或者:

$request->route()->getActionMethod()

 

 

本文:Laravel:在视图(view)中显示控制器(controller), Get Laravel controller name in view

Leave a Reply