且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在视图中获取 Laravel 5 控制器名称

更新时间:2023-11-26 10:36:46

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

If your layout is a Blade template, you could create a view composer that injects those variables into your layout. In app/Providers/AppServiceProvider.php add something like this:

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.

You will then have two variables available in your layout template: $controller and $action.