think\Loader::controller PHP Method

controller() public static method

实例化(分层)控制器 格式:[模块名/]控制器名
public static controller ( string $name, string $layer = 'controller', boolean $appendSuffix = false, string $empty = '' ) : Object | false
$name string 资源地址
$layer string 控制层名称
$appendSuffix boolean 是否添加类名后缀
$empty string 空控制器名称
return Object | false
    public static function controller($name, $layer = 'controller', $appendSuffix = false, $empty = '')
    {
        if (strpos($name, '/')) {
            list($module, $name) = explode('/', $name);
        } else {
            $module = Request::instance()->module();
        }
        $class = self::parseClass($module, $layer, $name, $appendSuffix);
        if (class_exists($class)) {
            return App::invokeClass($class);
        } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) {
            return new $emptyClass(Request::instance());
        }
    }

Usage Example

Example #1
0
 private static function module($result, $config)
 {
     if (APP_MULTI_MODULE) {
         // 多模块部署
         $module = strtolower($result[0] ?: $config['default_module']);
         // 获取模块名称
         define('MODULE_NAME', strip_tags($module));
         // 模块初始化
         if (MODULE_NAME && !in_array(MODULE_NAME, $config['deny_module_list']) && is_dir(APP_PATH . MODULE_NAME)) {
             define('MODULE_PATH', APP_PATH . MODULE_NAME . DS);
             define('VIEW_PATH', MODULE_PATH . VIEW_LAYER . DS);
             // 初始化模块
             self::initModule(MODULE_NAME, $config);
         } else {
             throw new Exception('module [ ' . MODULE_NAME . ' ] not exists ', 10005);
         }
     } else {
         // 单一模块部署
         define('MODULE_NAME', '');
         define('MODULE_PATH', APP_PATH);
         define('VIEW_PATH', MODULE_PATH . VIEW_LAYER . DS);
     }
     // 获取控制器名
     $controllerName = strip_tags($result[1] ?: Config::get('default_controller'));
     defined('CONTROLLER_NAME') or define('CONTROLLER_NAME', Config::get('url_controller_convert') ? strtolower($controllerName) : $controllerName);
     // 获取操作名
     $actionName = strip_tags($result[2] ?: Config::get('default_action'));
     defined('ACTION_NAME') or define('ACTION_NAME', Config::get('url_action_convert') ? strtolower($actionName) : $actionName);
     // 执行操作
     if (!preg_match('/^[A-Za-z](\\/|\\.|\\w)*$/', CONTROLLER_NAME)) {
         // 安全检测
         throw new Exception('illegal controller name:' . CONTROLLER_NAME, 10000);
     }
     $instance = Loader::controller(CONTROLLER_NAME, '', Config::get('use_controller_suffix'), Config::get('empty_controller'));
     // 获取当前操作名
     $action = ACTION_NAME . Config::get('action_suffix');
     try {
         // 操作方法开始监听
         $call = [$instance, $action];
         APP_HOOK && Hook::listen('action_begin', $call);
         if (!preg_match('/^[A-Za-z](\\w)*$/', $action)) {
             // 非法操作
             throw new \ReflectionException('illegal action name :' . ACTION_NAME);
         }
         // 执行操作方法
         $data = self::invokeMethod($call);
     } catch (\ReflectionException $e) {
         // 操作不存在
         if (method_exists($instance, '_empty')) {
             $method = new \ReflectionMethod($instance, '_empty');
             $data = $method->invokeArgs($instance, [$action, '']);
             APP_DEBUG && Log::record('[ RUN ] ' . $method->getFileName(), 'info');
         } else {
             throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002);
         }
     }
     return $data;
 }
All Usage Examples Of think\Loader::controller