|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: liu21st <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
declare (strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace think\route\dispatch; |
|
14
|
|
|
|
|
15
|
|
|
use ReflectionMethod; |
|
16
|
|
|
use think\App; |
|
17
|
|
|
use think\exception\ClassNotFoundException; |
|
18
|
|
|
use think\exception\HttpException; |
|
19
|
|
|
use think\Request; |
|
20
|
|
|
use think\route\Dispatch; |
|
21
|
|
|
|
|
22
|
|
|
class Controller extends Dispatch |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
protected $controller; |
|
25
|
|
|
protected $actionName; |
|
26
|
|
|
|
|
27
|
|
|
public function init() |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
parent::init(); |
|
30
|
|
|
|
|
31
|
|
|
$result = $this->dispatch; |
|
32
|
|
|
|
|
33
|
|
|
if (is_string($result)) { |
|
34
|
|
|
$result = explode('/', $result); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// 是否自动转换控制器和操作名 |
|
38
|
|
|
$convert = is_bool($this->convert) ? $this->convert : $this->rule->config('url_convert'); |
|
|
|
|
|
|
39
|
|
|
// 获取控制器名 |
|
40
|
|
|
$controller = strip_tags($result[0] ?: $this->rule->config('default_controller')); |
|
41
|
|
|
|
|
42
|
|
|
$this->controller = $convert ? strtolower($controller) : $controller; |
|
43
|
|
|
|
|
44
|
|
|
// 获取操作名 |
|
45
|
|
|
$this->actionName = strip_tags($result[1] ?: $this->rule->config('default_action')); |
|
46
|
|
|
|
|
47
|
|
|
// 设置当前请求的控制器、操作 |
|
48
|
|
|
$this->request |
|
49
|
|
|
->setController(App::parseName($this->controller, 1)) |
|
50
|
|
|
->setAction($this->actionName); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function exec() |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
|
|
// 实例化控制器 |
|
57
|
|
|
$instance = $this->controller($this->controller); |
|
58
|
|
|
} catch (ClassNotFoundException $e) { |
|
59
|
|
|
throw new HttpException(404, 'controller not exists:' . $e->getClass()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->app['middleware']->controller(function (Request $request, $next) use ($instance) { |
|
|
|
|
|
|
63
|
|
|
// 获取当前操作名 |
|
64
|
|
|
$action = $this->actionName . $this->rule->config('action_suffix'); |
|
65
|
|
|
|
|
66
|
|
|
if (is_callable([$instance, $action])) { |
|
67
|
|
|
// 严格获取当前操作方法名 |
|
68
|
|
|
$reflect = new ReflectionMethod($instance, $action); |
|
69
|
|
|
$actionName = $reflect->getName(); |
|
70
|
|
|
$this->request->setAction($actionName); |
|
71
|
|
|
|
|
72
|
|
|
// 自动获取请求变量 |
|
73
|
|
|
$vars = array_merge($this->request->param(), $this->param); |
|
|
|
|
|
|
74
|
|
|
} else { |
|
75
|
|
|
// 操作不存在 |
|
76
|
|
|
throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$data = $this->app->invokeReflectMethod($instance, $reflect, $vars); |
|
80
|
|
|
|
|
81
|
|
|
return $this->autoResponse($data); |
|
82
|
|
|
}); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
return $this->app['middleware']->dispatch($this->request, 'controller'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* 实例化访问控制器 |
|
89
|
|
|
* @access public |
|
90
|
|
|
* @param string $name 资源地址 |
|
91
|
|
|
* @return object |
|
92
|
|
|
* @throws ClassNotFoundException |
|
93
|
|
|
*/ |
|
94
|
|
|
public function controller(string $name) |
|
95
|
|
|
{ |
|
96
|
|
|
$suffix = $this->rule->config('controller_suffix') ? 'Controller' : ''; |
|
97
|
|
|
|
|
98
|
|
|
$controllerLayer = $this->rule->config('controller_layer') ?: 'controller'; |
|
99
|
|
|
$emptyController = $this->rule->config('empty_controller') ?: 'Error'; |
|
100
|
|
|
|
|
101
|
|
|
$class = $this->app->parseClass($controllerLayer, $name . $suffix); |
|
102
|
|
|
|
|
103
|
|
|
if (class_exists($class)) { |
|
104
|
|
|
return $this->app->make($class, [], true); |
|
105
|
|
|
} elseif ($emptyController && class_exists($emptyClass = $this->app->parseClass($controllerLayer, $emptyController . $suffix))) { |
|
106
|
|
|
return $this->app->make($emptyClass, [], true); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
throw new ClassNotFoundException('class not exists:' . $class, $class); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|