Route.php
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace think\addons;
use think\Config;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\Hook;
use think\Loader;
use think\Request;
use think\Response;
/**
* 插件执行默认控制器
* @package think\addons
*/
class Route
{
/**
* 插件执行
*/
public function execute($addon = null, $controller = null, $action = null)
{
$request = Request::instance();
// 是否自动转换控制器和操作名
$convert = Config::get('url_convert');
$filter = $convert ? 'strtolower' : 'trim';
$addon = $addon ? trim(call_user_func($filter, $addon)) : '';
$controller = $controller ? trim(call_user_func($filter, $controller)) : 'index';
$action = $action ? trim(call_user_func($filter, $action)) : 'index';
Hook::listen('addon_begin', $request);
if (!empty($addon) && !empty($controller) && !empty($action)) {
$info = get_addon_info($addon);
if (!$info) {
throw new HttpException(404, __('addon %s not found', $addon));
}
if (!$info['state']) {
throw new HttpException(500, __('addon %s is disabled', $addon));
}
$dispatch = $request->dispatch();
if (isset($dispatch['var']) && $dispatch['var']) {
$request->route(array_diff_key($dispatch['var'], array_flip(['addon', 'controller', 'action'])));
}
// 设置当前请求的控制器、操作
$request->controller($controller)->action($action);
// 监听addon_module_init
Hook::listen('addon_module_init', $request);
// 兼容旧版本行为,即将移除,不建议使用
Hook::listen('addons_init', $request);
$class = get_addon_class($addon, 'controller', $controller);
if (!$class) {
throw new HttpException(404, __('addon controller %s not found', Loader::parseName($controller, 1)));
}
$instance = new $class($request);
$vars = [];
if (is_callable([$instance, $action])) {
// 执行操作方法
$call = [$instance, $action];
} elseif (is_callable([$instance, '_empty'])) {
// 空操作
$call = [$instance, '_empty'];
$vars = [$action];
} else {
// 操作不存在
throw new HttpException(404, __('addon action %s not found', get_class($instance) . '->' . $action . '()'));
}
Hook::listen('addon_action_begin', $call);
return call_user_func_array($call, $vars);
} else {
abort(500, lang('addon can not be empty'));
}
}
}