mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
支持单独启用控制器类的类名后缀 CLASS_APPEND_SUFFIX 作为全局开关
This commit is contained in:
@@ -33,6 +33,8 @@ return [
|
|||||||
'default_filter' => '',
|
'default_filter' => '',
|
||||||
// 自动Response输出
|
// 自动Response输出
|
||||||
'response_auto_output' => true,
|
'response_auto_output' => true,
|
||||||
|
// 是否启用控制器类后缀
|
||||||
|
'use_controller_suffix' => false,
|
||||||
|
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | 模块设置
|
// | 模块设置
|
||||||
|
|||||||
@@ -227,7 +227,7 @@ class App
|
|||||||
// 安全检测
|
// 安全检测
|
||||||
throw new Exception('illegal controller name:' . CONTROLLER_NAME, 10000);
|
throw new Exception('illegal controller name:' . CONTROLLER_NAME, 10000);
|
||||||
}
|
}
|
||||||
$instance = Loader::controller(CONTROLLER_NAME, '', Config::get('empty_controller'));
|
$instance = Loader::controller(CONTROLLER_NAME, '', Config::get('use_controller_suffix'), Config::get('empty_controller'));
|
||||||
// 获取当前操作名
|
// 获取当前操作名
|
||||||
$action = ACTION_NAME . Config::get('action_suffix');
|
$action = ACTION_NAME . Config::get('action_suffix');
|
||||||
|
|
||||||
@@ -237,7 +237,7 @@ class App
|
|||||||
APP_HOOK && Hook::listen('action_begin', $call);
|
APP_HOOK && Hook::listen('action_begin', $call);
|
||||||
if (!preg_match('/^[A-Za-z](\w)*$/', $action)) {
|
if (!preg_match('/^[A-Za-z](\w)*$/', $action)) {
|
||||||
// 非法操作
|
// 非法操作
|
||||||
throw new Exception('illegal action name :' . ACTION_NAME, 10001);
|
throw new \ReflectionException('illegal action name :' . ACTION_NAME);
|
||||||
}
|
}
|
||||||
// 执行操作方法
|
// 执行操作方法
|
||||||
$data = self::invokeMethod($call);
|
$data = self::invokeMethod($call);
|
||||||
|
|||||||
@@ -263,9 +263,10 @@ class Loader
|
|||||||
* 实例化(分层)模型
|
* 实例化(分层)模型
|
||||||
* @param string $name Model名称
|
* @param string $name Model名称
|
||||||
* @param string $layer 业务层名称
|
* @param string $layer 业务层名称
|
||||||
|
* @param bool $appendSuffix 是否添加类名后缀
|
||||||
* @return Object
|
* @return Object
|
||||||
*/
|
*/
|
||||||
public static function model($name = '', $layer = MODEL_LAYER)
|
public static function model($name = '', $layer = MODEL_LAYER, $appendSuffix = false)
|
||||||
{
|
{
|
||||||
static $_model = [];
|
static $_model = [];
|
||||||
if (isset($_model[$name . $layer])) {
|
if (isset($_model[$name . $layer])) {
|
||||||
@@ -276,7 +277,7 @@ class Loader
|
|||||||
} else {
|
} else {
|
||||||
$module = APP_MULTI_MODULE ? MODULE_NAME : '';
|
$module = APP_MULTI_MODULE ? MODULE_NAME : '';
|
||||||
}
|
}
|
||||||
$class = self::parseClass($module, $layer, $name);
|
$class = self::parseClass($module, $layer, $name, $appendSuffix);
|
||||||
if (class_exists($class)) {
|
if (class_exists($class)) {
|
||||||
$model = new $class();
|
$model = new $class();
|
||||||
} else {
|
} else {
|
||||||
@@ -295,10 +296,11 @@ class Loader
|
|||||||
* 实例化(分层)控制器 格式:[模块名/]控制器名
|
* 实例化(分层)控制器 格式:[模块名/]控制器名
|
||||||
* @param string $name 资源地址
|
* @param string $name 资源地址
|
||||||
* @param string $layer 控制层名称
|
* @param string $layer 控制层名称
|
||||||
|
* @param bool $appendSuffix 是否添加类名后缀
|
||||||
* @param string $empty 空控制器名称
|
* @param string $empty 空控制器名称
|
||||||
* @return Object|false
|
* @return Object|false
|
||||||
*/
|
*/
|
||||||
public static function controller($name, $layer = '', $empty = '')
|
public static function controller($name, $layer = '', $appendSuffix = false, $empty = '')
|
||||||
{
|
{
|
||||||
static $_instance = [];
|
static $_instance = [];
|
||||||
$layer = $layer ?: CONTROLLER_LAYER;
|
$layer = $layer ?: CONTROLLER_LAYER;
|
||||||
@@ -310,12 +312,12 @@ class Loader
|
|||||||
} else {
|
} else {
|
||||||
$module = APP_MULTI_MODULE ? MODULE_NAME : '';
|
$module = APP_MULTI_MODULE ? MODULE_NAME : '';
|
||||||
}
|
}
|
||||||
$class = self::parseClass($module, $layer, $name);
|
$class = self::parseClass($module, $layer, $name, $appendSuffix);
|
||||||
if (class_exists($class)) {
|
if (class_exists($class)) {
|
||||||
$action = new $class;
|
$action = new $class;
|
||||||
$_instance[$name . $layer] = $action;
|
$_instance[$name . $layer] = $action;
|
||||||
return $action;
|
return $action;
|
||||||
} elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty))) {
|
} elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) {
|
||||||
return new $emptyClass;
|
return new $emptyClass;
|
||||||
} else {
|
} else {
|
||||||
throw new Exception('class [ ' . $class . ' ] not exists', 10001);
|
throw new Exception('class [ ' . $class . ' ] not exists', 10001);
|
||||||
@@ -326,9 +328,10 @@ class Loader
|
|||||||
* 实例化验证类 格式:[模块名/]验证器名
|
* 实例化验证类 格式:[模块名/]验证器名
|
||||||
* @param string $name 资源地址
|
* @param string $name 资源地址
|
||||||
* @param string $layer 验证层名称
|
* @param string $layer 验证层名称
|
||||||
|
* @param bool $appendSuffix 是否添加类名后缀
|
||||||
* @return Object|false
|
* @return Object|false
|
||||||
*/
|
*/
|
||||||
public static function validate($name = '', $layer = '')
|
public static function validate($name = '', $layer = '', $appendSuffix = false)
|
||||||
{
|
{
|
||||||
if (empty($name)) {
|
if (empty($name)) {
|
||||||
return new Validate;
|
return new Validate;
|
||||||
@@ -343,7 +346,7 @@ class Loader
|
|||||||
} else {
|
} else {
|
||||||
$module = APP_MULTI_MODULE ? MODULE_NAME : '';
|
$module = APP_MULTI_MODULE ? MODULE_NAME : '';
|
||||||
}
|
}
|
||||||
$class = self::parseClass($module, $layer, $name);
|
$class = self::parseClass($module, $layer, $name, $appendSuffix);
|
||||||
if (class_exists($class)) {
|
if (class_exists($class)) {
|
||||||
$validate = new $class;
|
$validate = new $class;
|
||||||
} else {
|
} else {
|
||||||
@@ -373,14 +376,15 @@ class Loader
|
|||||||
* @param string $url 调用地址
|
* @param string $url 调用地址
|
||||||
* @param string|array $vars 调用参数 支持字符串和数组
|
* @param string|array $vars 调用参数 支持字符串和数组
|
||||||
* @param string $layer 要调用的控制层名称
|
* @param string $layer 要调用的控制层名称
|
||||||
|
* @param bool $appendSuffix 是否添加类名后缀
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function action($url, $vars = [], $layer = CONTROLLER_LAYER)
|
public static function action($url, $vars = [], $layer = CONTROLLER_LAYER, $appendSuffix = false)
|
||||||
{
|
{
|
||||||
$info = pathinfo($url);
|
$info = pathinfo($url);
|
||||||
$action = $info['basename'];
|
$action = $info['basename'];
|
||||||
$module = '.' != $info['dirname'] ? $info['dirname'] : CONTROLLER_NAME;
|
$module = '.' != $info['dirname'] ? $info['dirname'] : CONTROLLER_NAME;
|
||||||
$class = self::controller($module, $layer);
|
$class = self::controller($module, $layer, $appendSuffix);
|
||||||
if ($class) {
|
if ($class) {
|
||||||
if (is_scalar($vars)) {
|
if (is_scalar($vars)) {
|
||||||
if (strpos($vars, '=')) {
|
if (strpos($vars, '=')) {
|
||||||
@@ -443,11 +447,11 @@ class Loader
|
|||||||
* @param string $name 类名
|
* @param string $name 类名
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function parseClass($module, $layer, $name)
|
public static function parseClass($module, $layer, $name, $appendSuffix = false)
|
||||||
{
|
{
|
||||||
$name = str_replace(['/', '.'], '\\', $name);
|
$name = str_replace(['/', '.'], '\\', $name);
|
||||||
$array = explode('\\', $name);
|
$array = explode('\\', $name);
|
||||||
$class = self::parseName(array_pop($array), 1) . (CLASS_APPEND_SUFFIX ? ucfirst($layer) : '');
|
$class = self::parseName(array_pop($array), 1) . (CLASS_APPEND_SUFFIX || $appendSuffix ? ucfirst($layer) : '');
|
||||||
$path = $array ? implode('\\', $array) . '\\' : '';
|
$path = $array ? implode('\\', $array) . '\\' : '';
|
||||||
return APP_NAMESPACE . '\\' . (APP_MULTI_MODULE ? $module . '\\' : '') . $layer . '\\' . $path . $class;
|
return APP_NAMESPACE . '\\' . (APP_MULTI_MODULE ? $module . '\\' : '') . $layer . '\\' . $path . $class;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user