添加网站文件
This commit is contained in:
26
thinkphp/library/think/route/dispatch/Callback.php
Normal file
26
thinkphp/library/think/route/dispatch/Callback.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\route\dispatch;
|
||||
|
||||
use think\route\Dispatch;
|
||||
|
||||
class Callback extends Dispatch
|
||||
{
|
||||
public function exec()
|
||||
{
|
||||
// 执行回调方法
|
||||
$vars = array_merge($this->request->param(), $this->param);
|
||||
|
||||
return $this->app->invoke($this->dispatch, $vars);
|
||||
}
|
||||
|
||||
}
|
||||
30
thinkphp/library/think/route/dispatch/Controller.php
Normal file
30
thinkphp/library/think/route/dispatch/Controller.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\route\dispatch;
|
||||
|
||||
use think\route\Dispatch;
|
||||
|
||||
class Controller extends Dispatch
|
||||
{
|
||||
public function exec()
|
||||
{
|
||||
// 执行控制器的操作方法
|
||||
$vars = array_merge($this->request->param(), $this->param);
|
||||
|
||||
return $this->app->action(
|
||||
$this->dispatch, $vars,
|
||||
$this->rule->getConfig('url_controller_layer'),
|
||||
$this->rule->getConfig('controller_suffix')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
138
thinkphp/library/think/route/dispatch/Module.php
Normal file
138
thinkphp/library/think/route/dispatch/Module.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\route\dispatch;
|
||||
|
||||
use ReflectionMethod;
|
||||
use think\exception\ClassNotFoundException;
|
||||
use think\exception\HttpException;
|
||||
use think\Loader;
|
||||
use think\Request;
|
||||
use think\route\Dispatch;
|
||||
|
||||
class Module extends Dispatch
|
||||
{
|
||||
protected $controller;
|
||||
protected $actionName;
|
||||
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
|
||||
$result = $this->dispatch;
|
||||
|
||||
if (is_string($result)) {
|
||||
$result = explode('/', $result);
|
||||
}
|
||||
|
||||
if ($this->rule->getConfig('app_multi_module')) {
|
||||
// 多模块部署
|
||||
$module = strip_tags(strtolower($result[0] ?: $this->rule->getConfig('default_module')));
|
||||
$bind = $this->rule->getRouter()->getBind();
|
||||
$available = false;
|
||||
|
||||
if ($bind && preg_match('/^[a-z]/is', $bind)) {
|
||||
// 绑定模块
|
||||
list($bindModule) = explode('/', $bind);
|
||||
if (empty($result[0])) {
|
||||
$module = $bindModule;
|
||||
}
|
||||
$available = true;
|
||||
} elseif (!in_array($module, $this->rule->getConfig('deny_module_list')) && is_dir($this->app->getAppPath() . $module)) {
|
||||
$available = true;
|
||||
} elseif ($this->rule->getConfig('empty_module')) {
|
||||
$module = $this->rule->getConfig('empty_module');
|
||||
$available = true;
|
||||
}
|
||||
|
||||
// 模块初始化
|
||||
if ($module && $available) {
|
||||
// 初始化模块
|
||||
$this->request->setModule($module);
|
||||
$this->app->init($module);
|
||||
} else {
|
||||
throw new HttpException(404, 'module not exists:' . $module);
|
||||
}
|
||||
}
|
||||
|
||||
// 是否自动转换控制器和操作名
|
||||
$convert = is_bool($this->convert) ? $this->convert : $this->rule->getConfig('url_convert');
|
||||
// 获取控制器名
|
||||
$controller = strip_tags($result[1] ?: $this->rule->getConfig('default_controller'));
|
||||
|
||||
$this->controller = $convert ? strtolower($controller) : $controller;
|
||||
|
||||
// 获取操作名
|
||||
$this->actionName = strip_tags($result[2] ?: $this->rule->getConfig('default_action'));
|
||||
|
||||
// 设置当前请求的控制器、操作
|
||||
$this->request
|
||||
->setController(Loader::parseName($this->controller, 1))
|
||||
->setAction($this->actionName);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function exec()
|
||||
{
|
||||
// 监听module_init
|
||||
$this->app['hook']->listen('module_init');
|
||||
|
||||
try {
|
||||
// 实例化控制器
|
||||
$instance = $this->app->controller($this->controller,
|
||||
$this->rule->getConfig('url_controller_layer'),
|
||||
$this->rule->getConfig('controller_suffix'),
|
||||
$this->rule->getConfig('empty_controller'));
|
||||
} catch (ClassNotFoundException $e) {
|
||||
throw new HttpException(404, 'controller not exists:' . $e->getClass());
|
||||
}
|
||||
|
||||
$this->app['middleware']->controller(function (Request $request, $next) use ($instance) {
|
||||
// 获取当前操作名
|
||||
$action = $this->actionName . $this->rule->getConfig('action_suffix');
|
||||
|
||||
if (is_callable([$instance, $action])) {
|
||||
// 执行操作方法
|
||||
$call = [$instance, $action];
|
||||
|
||||
// 严格获取当前操作方法名
|
||||
$reflect = new ReflectionMethod($instance, $action);
|
||||
$methodName = $reflect->getName();
|
||||
$suffix = $this->rule->getConfig('action_suffix');
|
||||
$actionName = $suffix ? substr($methodName, 0, -strlen($suffix)) : $methodName;
|
||||
$this->request->setAction($actionName);
|
||||
|
||||
// 自动获取请求变量
|
||||
$vars = $this->rule->getConfig('url_param_type')
|
||||
? $this->request->route()
|
||||
: $this->request->param();
|
||||
$vars = array_merge($vars, $this->param);
|
||||
} elseif (is_callable([$instance, '_empty'])) {
|
||||
// 空操作
|
||||
$call = [$instance, '_empty'];
|
||||
$vars = [$this->actionName];
|
||||
$reflect = new ReflectionMethod($instance, '_empty');
|
||||
} else {
|
||||
// 操作不存在
|
||||
throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()');
|
||||
}
|
||||
|
||||
$this->app['hook']->listen('action_begin', $call);
|
||||
|
||||
$data = $this->app->invokeReflectMethod($instance, $reflect, $vars);
|
||||
|
||||
return $this->autoResponse($data);
|
||||
});
|
||||
|
||||
return $this->app['middleware']->dispatch($this->request, 'controller');
|
||||
}
|
||||
}
|
||||
23
thinkphp/library/think/route/dispatch/Redirect.php
Normal file
23
thinkphp/library/think/route/dispatch/Redirect.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\route\dispatch;
|
||||
|
||||
use think\Response;
|
||||
use think\route\Dispatch;
|
||||
|
||||
class Redirect extends Dispatch
|
||||
{
|
||||
public function exec()
|
||||
{
|
||||
return Response::create($this->dispatch, 'redirect')->code($this->code);
|
||||
}
|
||||
}
|
||||
23
thinkphp/library/think/route/dispatch/Response.php
Normal file
23
thinkphp/library/think/route/dispatch/Response.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\route\dispatch;
|
||||
|
||||
use think\route\Dispatch;
|
||||
|
||||
class Response extends Dispatch
|
||||
{
|
||||
public function exec()
|
||||
{
|
||||
return $this->dispatch;
|
||||
}
|
||||
|
||||
}
|
||||
169
thinkphp/library/think/route/dispatch/Url.php
Normal file
169
thinkphp/library/think/route/dispatch/Url.php
Normal file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\route\dispatch;
|
||||
|
||||
use think\exception\HttpException;
|
||||
use think\Loader;
|
||||
use think\route\Dispatch;
|
||||
|
||||
class Url extends Dispatch
|
||||
{
|
||||
public function init()
|
||||
{
|
||||
// 解析默认的URL规则
|
||||
$result = $this->parseUrl($this->dispatch);
|
||||
|
||||
return (new Module($this->request, $this->rule, $result))->init();
|
||||
}
|
||||
|
||||
public function exec()
|
||||
{}
|
||||
|
||||
/**
|
||||
* 解析URL地址
|
||||
* @access protected
|
||||
* @param string $url URL
|
||||
* @return array
|
||||
*/
|
||||
protected function parseUrl($url)
|
||||
{
|
||||
$depr = $this->rule->getConfig('pathinfo_depr');
|
||||
$bind = $this->rule->getRouter()->getBind();
|
||||
|
||||
if (!empty($bind) && preg_match('/^[a-z]/is', $bind)) {
|
||||
$bind = str_replace('/', $depr, $bind);
|
||||
// 如果有模块/控制器绑定
|
||||
$url = $bind . ('.' != substr($bind, -1) ? $depr : '') . ltrim($url, $depr);
|
||||
}
|
||||
|
||||
list($path, $var) = $this->rule->parseUrlPath($url);
|
||||
if (empty($path)) {
|
||||
return [null, null, null];
|
||||
}
|
||||
|
||||
// 解析模块
|
||||
$module = $this->rule->getConfig('app_multi_module') ? array_shift($path) : null;
|
||||
|
||||
if ($this->param['auto_search']) {
|
||||
$controller = $this->autoFindController($module, $path);
|
||||
} else {
|
||||
// 解析控制器
|
||||
$controller = !empty($path) ? array_shift($path) : null;
|
||||
}
|
||||
|
||||
if ($controller && !preg_match('/^[A-Za-z0-9][\w|\.]*$/', $controller)) {
|
||||
throw new HttpException(404, 'controller not exists:' . $controller);
|
||||
}
|
||||
|
||||
// 解析操作
|
||||
$action = !empty($path) ? array_shift($path) : null;
|
||||
|
||||
// 解析额外参数
|
||||
if ($path) {
|
||||
if ($this->rule->getConfig('url_param_type')) {
|
||||
$var += $path;
|
||||
} else {
|
||||
preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) {
|
||||
$var[$match[1]] = strip_tags($match[2]);
|
||||
}, implode('|', $path));
|
||||
}
|
||||
}
|
||||
|
||||
$panDomain = $this->request->panDomain();
|
||||
|
||||
if ($panDomain && $key = array_search('*', $var)) {
|
||||
// 泛域名赋值
|
||||
$var[$key] = $panDomain;
|
||||
}
|
||||
|
||||
// 设置当前请求的参数
|
||||
$this->request->setRouteVars($var);
|
||||
|
||||
// 封装路由
|
||||
$route = [$module, $controller, $action];
|
||||
|
||||
if ($this->hasDefinedRoute($route, $bind)) {
|
||||
throw new HttpException(404, 'invalid request:' . str_replace('|', $depr, $url));
|
||||
}
|
||||
|
||||
return $route;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查URL是否已经定义过路由
|
||||
* @access protected
|
||||
* @param string $route 路由信息
|
||||
* @param string $bind 绑定信息
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasDefinedRoute($route, $bind)
|
||||
{
|
||||
list($module, $controller, $action) = $route;
|
||||
|
||||
// 检查地址是否被定义过路由
|
||||
$name = strtolower($module . '/' . Loader::parseName($controller, 1) . '/' . $action);
|
||||
|
||||
$name2 = '';
|
||||
|
||||
if (empty($module) || $module == $bind) {
|
||||
$name2 = strtolower(Loader::parseName($controller, 1) . '/' . $action);
|
||||
}
|
||||
|
||||
$host = $this->request->host(true);
|
||||
|
||||
$method = $this->request->method();
|
||||
|
||||
if ($this->rule->getRouter()->getName($name, $host, $method) || $this->rule->getRouter()->getName($name2, $host, $method)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动定位控制器类
|
||||
* @access protected
|
||||
* @param string $module 模块名
|
||||
* @param array $path URL
|
||||
* @return string
|
||||
*/
|
||||
protected function autoFindController($module, &$path)
|
||||
{
|
||||
$dir = $this->app->getAppPath() . ($module ? $module . '/' : '') . $this->rule->getConfig('url_controller_layer');
|
||||
$suffix = $this->app->getSuffix() || $this->rule->getConfig('controller_suffix') ? ucfirst($this->rule->getConfig('url_controller_layer')) : '';
|
||||
|
||||
$item = [];
|
||||
$find = false;
|
||||
|
||||
foreach ($path as $val) {
|
||||
$item[] = $val;
|
||||
$file = $dir . '/' . str_replace('.', '/', $val) . $suffix . '.php';
|
||||
$file = pathinfo($file, PATHINFO_DIRNAME) . '/' . Loader::parseName(pathinfo($file, PATHINFO_FILENAME), 1) . '.php';
|
||||
if (is_file($file)) {
|
||||
$find = true;
|
||||
break;
|
||||
} else {
|
||||
$dir .= '/' . Loader::parseName($val);
|
||||
}
|
||||
}
|
||||
|
||||
if ($find) {
|
||||
$controller = implode('.', $item);
|
||||
$path = array_slice($path, count($item));
|
||||
} else {
|
||||
$controller = array_shift($path);
|
||||
}
|
||||
|
||||
return $controller;
|
||||
}
|
||||
|
||||
}
|
||||
26
thinkphp/library/think/route/dispatch/View.php
Normal file
26
thinkphp/library/think/route/dispatch/View.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\route\dispatch;
|
||||
|
||||
use think\Response;
|
||||
use think\route\Dispatch;
|
||||
|
||||
class View extends Dispatch
|
||||
{
|
||||
public function exec()
|
||||
{
|
||||
// 渲染模板输出
|
||||
$vars = array_merge($this->request->param(), $this->param);
|
||||
|
||||
return Response::create($this->dispatch, 'view')->assign($vars);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user