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
Exemplo n.º 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;
 }
Exemplo n.º 2
0
/**
 * 实例化控制器 格式:[模块/]控制器
 * @param string $name 资源地址
 * @param string $layer 控制层名称
 * @return object
 */
function A($name, $layer = CONTROLLER_LAYER)
{
    return \think\Loader::controller($name, $layer);
}
Exemplo n.º 3
0
 /**
  * 实例化控制器 格式:[模块/]控制器
  * @param string    $name 资源地址
  * @param string    $layer 控制层名称
  * @param bool      $appendSuffix 是否添加类名后缀
  * @return \think\Controller
  */
 function controller($name, $layer = 'controller', $appendSuffix = false)
 {
     return Loader::controller($name, $layer, $appendSuffix);
 }
Exemplo n.º 4
0
Arquivo: App.php Projeto: GDdark/cici
 /**
  * 执行模块
  * @access public
  * @param array $result 模块/控制器/操作
  * @param array $config 配置参数
  * @param bool  $convert 是否自动转换控制器和操作名
  * @return mixed
  */
 public static function module($result, $config, $convert = null)
 {
     if (is_string($result)) {
         $result = explode('/', $result);
     }
     $request = Request::instance();
     if ($config['app_multi_module']) {
         // 多模块部署
         $module = strip_tags(strtolower($result[0] ?: $config['default_module']));
         $bind = Route::getBind('module');
         $available = false;
         if ($bind) {
             // 绑定模块
             list($bindModule) = explode('/', $bind);
             if ($module == $bindModule) {
                 $available = true;
             }
         } elseif (!in_array($module, $config['deny_module_list']) && is_dir(APP_PATH . $module)) {
             $available = true;
         }
         // 模块初始化
         if ($module && $available) {
             // 初始化模块
             $request->module($module);
             $config = self::init($module);
         } else {
             throw new HttpException(404, 'module not exists:' . $module);
         }
     } else {
         // 单一模块部署
         $module = '';
         $request->module($module);
     }
     // 当前模块路径
     App::$modulePath = APP_PATH . ($module ? $module . DS : '');
     // 是否自动转换控制器和操作名
     $convert = is_bool($convert) ? $convert : $config['url_convert'];
     // 获取控制器名
     $controller = strip_tags($result[1] ?: $config['default_controller']);
     $controller = $convert ? strtolower($controller) : $controller;
     // 获取操作名
     $actionName = strip_tags($result[2] ?: $config['default_action']);
     $actionName = $convert ? strtolower($actionName) : $actionName;
     // 设置当前请求的控制器、操作
     $request->controller($controller)->action($actionName);
     // 监听module_init
     Hook::listen('module_init', $request);
     try {
         $instance = Loader::controller($controller, $config['url_controller_layer'], $config['controller_suffix'], $config['empty_controller']);
         if (is_null($instance)) {
             throw new HttpException(404, 'controller not exists:' . $controller);
         }
         // 获取当前操作名
         $action = $actionName . $config['action_suffix'];
         if (!preg_match('/^[A-Za-z](\\w)*$/', $action)) {
             // 非法操作
             throw new \ReflectionException('illegal action name:' . $actionName);
         }
         // 执行操作方法
         $call = [$instance, $action];
         Hook::listen('action_begin', $call);
         $data = self::invokeMethod($call);
     } catch (\ReflectionException $e) {
         // 操作不存在
         if (method_exists($instance, '_empty')) {
             $method = new \ReflectionMethod($instance, '_empty');
             $data = $method->invokeArgs($instance, [$action, '']);
             self::$debug && Log::record('[ RUN ] ' . $method->__toString(), 'info');
         } else {
             throw new HttpException(404, 'method not exists:' . (new \ReflectionClass($instance))->getName() . '->' . $action);
         }
     }
     return $data;
 }
Exemplo n.º 5
0
/**
 * 实例化控制器 格式:[模块/]控制器
 * @param string $name 资源地址
 * @param string $layer 控制层名称
 * @return \think\Controller
 */
function controller($name, $layer = CONTROLLER_LAYER)
{
    return Loader::controller($name, $layer);
}
Exemplo n.º 6
0
 /**
  * 执行应用程序
  * @access public
  * @return void
  */
 public static function run($config)
 {
     // 日志初始化
     Log::init($config['log']);
     // 缓存初始化
     Cache::connect($config['cache']);
     // 加载框架底层语言包
     if (is_file(THINK_PATH . 'Lang/' . strtolower($config['default_lang']) . EXT)) {
         Lang::set(include THINK_PATH . 'Lang/' . strtolower($config['default_lang']) . EXT);
     }
     if (is_file(APP_PATH . 'build.php')) {
         // 自动化创建脚本
         Create::build(include APP_PATH . 'build.php');
     }
     // 监听app_init
     Hook::listen('app_init');
     // 初始化公共模块
     define('COMMON_PATH', APP_PATH . $config['common_module'] . '/');
     self::initModule(COMMON_PATH, $config);
     // 启动session
     if (!IS_CLI) {
         Session::init($config['session']);
     }
     // 应用URL调度
     self::dispatch($config);
     // 监听app_run
     Hook::listen('app_run');
     // 执行操作
     if (!preg_match('/^[A-Za-z](\\/|\\w)*$/', CONTROLLER_NAME)) {
         // 安全检测
         $instance = false;
     } elseif ($config['action_bind_class']) {
         // 操作绑定到类:模块\controller\控制器\操作
         if (is_dir(MODULE_PATH . CONTROLLER_LAYER . '/' . CONTROLLER_NAME)) {
             $namespace = MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . CONTROLLER_NAME . '\\';
         } else {
             // 空控制器
             $namespace = MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\empty\\';
         }
         $actionName = strtolower(ACTION_NAME);
         if (class_exists($namespace . $actionName)) {
             $class = $namespace . $actionName;
         } elseif (class_exists($namespace . '_empty')) {
             // 空操作
             $class = $namespace . '_empty';
         } else {
             throw new Exception('_ERROR_ACTION_:' . ACTION_NAME);
         }
         $instance = new $class();
         // 操作绑定到类后 固定执行run入口
         $action = 'run';
     } else {
         $instance = Loader::controller(CONTROLLER_NAME);
         // 获取当前操作名
         $action = ACTION_NAME . $config['action_suffix'];
     }
     if (!$instance) {
         throw new Exception('[ ' . MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . Loader::parseName(CONTROLLER_NAME, 1) . ' ] not exists');
     }
     try {
         // 操作方法开始监听
         $call = [$instance, $action];
         Hook::listen('action_begin', $call);
         if (!preg_match('/^[A-Za-z](\\w)*$/', $action)) {
             // 非法操作
             throw new \ReflectionException();
         }
         //执行当前操作
         $method = new \ReflectionMethod($instance, $action);
         if ($method->isPublic()) {
             // URL参数绑定检测
             if ($config['url_params_bind'] && $method->getNumberOfParameters() > 0) {
                 switch ($_SERVER['REQUEST_METHOD']) {
                     case 'POST':
                         $vars = array_merge($_GET, $_POST);
                         break;
                     case 'PUT':
                         parse_str(file_get_contents('php://input'), $vars);
                         break;
                     default:
                         $vars = $_GET;
                 }
                 $params = $method->getParameters();
                 $paramsBindType = $config['url_parmas_bind_type'];
                 foreach ($params as $param) {
                     $name = $param->getName();
                     if (1 == $paramsBindType && !empty($vars)) {
                         $args[] = array_shift($vars);
                     }
                     if (0 == $paramsBindType && isset($vars[$name])) {
                         $args[] = $vars[$name];
                     } elseif ($param->isDefaultValueAvailable()) {
                         $args[] = $param->getDefaultValue();
                     } else {
                         throw new Exception('_PARAM_ERROR_:' . $name);
                     }
                 }
                 array_walk_recursive($args, 'Input::filterExp');
                 $data = $method->invokeArgs($instance, $args);
             } else {
                 $data = $method->invoke($instance);
             }
             // 操作方法执行完成监听
             Hook::listen('action_end', $call);
             if (IS_API) {
                 // API接口返回数据
                 self::returnData($data, $config['default_ajax_return']);
             }
         } else {
             // 操作方法不是Public 抛出异常
             throw new \ReflectionException();
         }
     } catch (\ReflectionException $e) {
         // 操作不存在
         if (method_exists($instance, '_empty')) {
             $method = new \ReflectionMethod($instance, '_empty');
             $method->invokeArgs($instance, [$action, '']);
         } else {
             throw new Exception('[ ' . (new \ReflectionClass($instance))->getName() . ':' . $action . ' ] not exists ', 404);
         }
     }
     // 监听app_end
     Hook::listen('app_end');
     return;
 }
Exemplo n.º 7
0
 /**
  * 执行模块
  * @access public
  * @param array $result 模块/控制器/操作
  * @param array $config 配置参数
  * @param bool  $convert 是否自动转换控制器和操作名
  * @return mixed
  */
 public static function module($result, $config, $convert = null)
 {
     if (is_string($result)) {
         $result = explode('/', $result);
     }
     $request = Request::instance();
     if ($config['app_multi_module']) {
         // 多模块部署
         $module = strip_tags(strtolower($result[0] ?: $config['default_module']));
         $bind = Route::getBind('module');
         $available = false;
         if ($bind) {
             // 绑定模块
             list($bindModule) = explode('/', $bind);
             if (empty($result[0])) {
                 $module = $bindModule;
                 $available = true;
             } elseif ($module == $bindModule) {
                 $available = true;
             }
         } elseif (!in_array($module, $config['deny_module_list']) && is_dir(APP_PATH . $module)) {
             $available = true;
         }
         // 模块初始化
         if ($module && $available) {
             // 初始化模块
             $request->module($module);
             $config = self::init($module);
         } else {
             throw new HttpException(404, 'module not exists:' . $module);
         }
     } else {
         // 单一模块部署
         $module = '';
         $request->module($module);
     }
     // 当前模块路径
     App::$modulePath = APP_PATH . ($module ? $module . DS : '');
     // 是否自动转换控制器和操作名
     $convert = is_bool($convert) ? $convert : $config['url_convert'];
     // 获取控制器名
     $controller = strip_tags($result[1] ?: $config['default_controller']);
     $controller = $convert ? strtolower($controller) : $controller;
     // 获取操作名
     $actionName = strip_tags($result[2] ?: $config['default_action']);
     $actionName = $convert ? strtolower($actionName) : $actionName;
     // 设置当前请求的控制器、操作
     $request->controller(Loader::parseName($controller, 1))->action($actionName);
     // 监听module_init
     Hook::listen('module_init', $request);
     $instance = Loader::controller($controller, $config['url_controller_layer'], $config['controller_suffix'], $config['empty_controller']);
     if (is_null($instance)) {
         throw new HttpException(404, 'controller not exists:' . Loader::parseName($controller, 1));
     }
     // 获取当前操作名
     $action = $actionName . $config['action_suffix'];
     if (is_callable([$instance, $action])) {
         // 执行操作方法
         $call = [$instance, $action];
     } elseif (is_callable([$instance, '_empty'])) {
         // 空操作
         $call = [$instance, '_empty'];
     } else {
         // 操作不存在
         throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()');
     }
     Hook::listen('action_begin', $call);
     $data = self::invokeMethod($call);
     return $data;
 }