Skip to content

Commit

Permalink
修改get_addons_class方法支持controller类
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaobo.sun committed Oct 31, 2016
1 parent 328b4a0 commit 747d7c6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"homepage": "https://github.com/5ini99/think-addons",
"license": "Apache-2.0",
"minimum-stability": "stable",
"version": "1.1.0",
"version": "1.1.1",
"authors": [
{
"name": "xiaobo.sun",
Expand Down
19 changes: 11 additions & 8 deletions src/AddonsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ public function execute()
{
if (!empty($this->addon) && !empty($this->controller) && !empty($this->action)) {
// 获取类的命名空间
$class = get_addon_class($this->addon, 'controller') . "\\{$this->controller}";
$model = new $class();
if ($model === false) {
return $this->error(lang('addon init fail'));
$class = get_addon_class($this->addon, 'controller', $this->controller);
if(class_exists($class)) {
$model = new $class();
if ($model === false) {
$this->error(lang('addon init fail'));
}
// 调用操作
return call_user_func([$model, $this->action]);
}else{
$this->error(lang('Controller Class Not Exists'));
}
// 调用操作
return call_user_func([$model, $this->action]);
}

return $this->error(lang('addon cannot name or action'));
$this->error(lang('addon cannot name or action'));
}
}
13 changes: 8 additions & 5 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ function hook($hook, $params = [])
* 获取插件类的类名
* @param $name 插件名
* @param string $type 返回命名空间类型
* @param string $class 当前类名
* @return string
*/
function get_addon_class($name, $type = 'hook')
function get_addon_class($name, $type = 'hook', $class = null)
{
$name = \think\Loader::parseName($name);
$class = \think\Loader::parseName(is_null($class) ? $name : $class, 1);
switch ($type) {
case 'controller':
$namespace = "\\addons\\" . strtolower($name) . "\\controller\\";
$namespace = "\\addons\\" . $name . "\\controller\\" . $class;
break;
default:
$namespace = "\\addons\\" . strtolower($name) . "\\" . ucfirst(strtolower($name));
$namespace = "\\addons\\" . $name . "\\" . $class;
}

return class_exists($namespace) ? $namespace : '';
Expand All @@ -100,8 +103,8 @@ function get_addon_config($name)
* @param $url
* @param array $param
* @return bool|string
* @param bool|string $suffix 生成的URL后缀
* @param bool|string $domain 域名
* @param bool|string $suffix 生成的URL后缀
* @param bool|string $domain 域名
*/
function addon_url($url, $param = [], $suffix = true, $domain = false)
{
Expand Down

0 comments on commit 747d7c6

Please sign in to comment.