jqh před 4 roky
rodič
revize
a5695c3732
1 změnil soubory, kde provedl 26 přidání a 0 odebrání
  1. 26 0
      src/Support/Helper.php

+ 26 - 0
src/Support/Helper.php

@@ -34,6 +34,8 @@ class Helper
         'video'      => 'mkv|rmvb|flv|mp4|avi|wmv|rm|asf|mpeg',
     ];
 
+    protected static $controllerNames = [];
+
     /**
      * 把给定的值转化为数组.
      *
@@ -111,6 +113,30 @@ class Helper
         return (string) $value;
     }
 
+    /**
+     * 获取当前控制器名称.
+     *
+     * @return mixed|string
+     */
+    public static function getControllerName()
+    {
+        $router = app('router');
+
+        if (! $router->current()) {
+            return 'undefined';
+        }
+
+        $actionName = $router->current()->getActionName();
+
+        if (! isset(static::$controllerNames[$actionName])) {
+            $controller = class_basename(explode('@', $actionName)[0]);
+
+            static::$controllerNames[$actionName] = str_replace('Controller', '', $controller);
+        }
+
+        return static::$controllerNames[$actionName];
+    }
+
     /**
      * @param array $attributes
      *