在视图中获取Laravel 5控制器名称 [英] Get Laravel 5 controller name in view

查看:456
本文介绍了在视图中获取Laravel 5控制器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的旧网站CSS设置为,使用Zend Framework 1,使body标记具有控制器名称的ID和操作名称的类,现在我们切换到Laravel5.我找到了一种方法通过Route类获取操作名称,但找不到控制器名称的方法.我在Laravel文档中什么都看不到.有什么想法吗?

Our old website CSS was set up so that the body tag had an id of the controller name and a class of the action name, using Zend Framework 1. Now we're switching to Laravel 5. I found a way to get the action name through the Route class, but can't find a method for the controller name. I don't see anything in the Laravel docs like this. Any ideas?

这就是您采取行动的方式.您注入Route类,然后调用:

This is how you do with action. You inject the Route class, and then call:

$route->getActionName().

我正在为控制器寻找类似的东西.我检查了整个路线课程,却一无所获.

I'm looking for something similar for controllers. I've checked the entire route class and found nothing.

推荐答案

如果布局是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.

这篇关于在视图中获取Laravel 5控制器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆