App类改进run方法传入Request对象 便于构造自己的Request对象

This commit is contained in:
thinkphp
2016-05-11 12:25:29 +08:00
parent 9843eaf243
commit b50eeb5e7d
4 changed files with 25 additions and 24 deletions

View File

@@ -23,10 +23,12 @@ class App
/** /**
* 执行应用程序 * 执行应用程序
* @access public * @access public
* @param \think\Request $request Request对象
* @return void * @return void
*/ */
public static function run() public static function run($request)
{ {
// 初始化应用(公共模块) // 初始化应用(公共模块)
self::initModule(COMMON_MODULE, Config::get()); self::initModule(COMMON_MODULE, Config::get());
@@ -66,10 +68,10 @@ class App
} }
// 获取当前请求的调度信息 // 获取当前请求的调度信息
$dispatch = Request::instance()->dispatch(); $dispatch = $request->dispatch();
if (empty($dispatch)) { if (empty($dispatch)) {
// 未指定调度类型 则进行URL路由检测 // 未指定调度类型 则进行URL路由检测
$dispatch = self::route($config); $dispatch = self::route($request, $config);
} }
// 记录路由信息 // 记录路由信息
APP_DEBUG && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info'); APP_DEBUG && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info');
@@ -308,21 +310,22 @@ class App
/** /**
* URL路由检测根据PATH_INFO) * URL路由检测根据PATH_INFO)
* @access public * @access public
* @param \think\Request $request
* @param array $config * @param array $config
* @throws Exception * @throws Exception
*/ */
public static function route(array $config) public static function route($request, array $config)
{ {
define('__INFO__', Request::instance()->pathinfo()); define('__INFO__', $request->pathinfo());
define('__EXT__', Request::instance()->ext()); define('__EXT__', $request->ext());
// 检测URL禁用后缀 // 检测URL禁用后缀
if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) { if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) {
throw new Exception('url suffix deny'); throw new Exception('url suffix deny');
} }
$_SERVER['PATH_INFO'] = Request::instance()->path(); $_SERVER['PATH_INFO'] = $request->path();
$depr = $config['pathinfo_depr']; $depr = $config['pathinfo_depr'];
$result = false; $result = false;
// 路由检测 // 路由检测
@@ -333,7 +336,7 @@ class App
Route::register($config['route']); Route::register($config['route']);
} }
// 路由检测根据路由定义返回不同的URL调度 // 路由检测根据路由定义返回不同的URL调度
$result = Route::check($_SERVER['PATH_INFO'], $depr, !IS_CLI ? $config['url_domain_deploy'] : false); $result = Route::check($request, $_SERVER['PATH_INFO'], $depr, !IS_CLI ? $config['url_domain_deploy'] : false);
if (APP_ROUTE_MUST && false === $result && $config['url_route_must']) { if (APP_ROUTE_MUST && false === $result && $config['url_route_must']) {
// 路由无效 // 路由无效
throw new Exception('route not define '); throw new Exception('route not define ');
@@ -346,7 +349,7 @@ class App
//保证$_REQUEST正常取值 //保证$_REQUEST正常取值
$_REQUEST = array_merge($_POST, $_GET, $_COOKIE); $_REQUEST = array_merge($_POST, $_GET, $_COOKIE);
// 注册调度机制 // 注册调度机制
return Request::instance()->dispatch($result); return $request->dispatch($result);
} }
} }

View File

@@ -11,8 +11,6 @@
namespace think; namespace think;
use think\Request;
class Route class Route
{ {
// 路由规则 // 路由规则
@@ -358,7 +356,7 @@ class Route
} }
// 检测URL路由 // 检测URL路由
public static function check($url, $depr = '/', $checkDomain = false) public static function check($request, $url, $depr = '/', $checkDomain = false)
{ {
// 检测域名部署 // 检测域名部署
if ($checkDomain) { if ($checkDomain) {
@@ -429,7 +427,7 @@ class Route
} }
$result = self::checkRule($key, $route, $url1, $pattern, $option); $result = self::checkRule($key, $route, $url1, $pattern, $option);
if (false !== $result) { if (false !== $result) {
Request::instance()->route(['rule' => $key, 'route' => $route, 'pattern' => $pattern, 'option' => $option]); $request->route(['rule' => $key, 'route' => $route, 'pattern' => $pattern, 'option' => $option]);
return $result; return $result;
} }
} }
@@ -442,7 +440,7 @@ class Route
// 规则路由 // 规则路由
$result = self::checkRule($rule, $route, $url, $pattern, $option); $result = self::checkRule($rule, $route, $url, $pattern, $option);
if (false !== $result) { if (false !== $result) {
Request::instance()->route(['rule' => $rule, 'route' => $route, 'pattern' => $pattern, 'option' => $option]); $request->route(['rule' => $rule, 'route' => $route, 'pattern' => $pattern, 'option' => $option]);
return $result; return $result;
} }
} }

View File

@@ -64,5 +64,5 @@ if (APP_HOOK && isset($mode['tags'])) {
// 是否自动运行 // 是否自动运行
if (APP_AUTO_RUN) { if (APP_AUTO_RUN) {
App::run(); App::run(new Request());
} }

View File

@@ -26,17 +26,17 @@ class urlTest extends \PHPUnit_Framework_TestCase
public function testBuildModule() public function testBuildModule()
{ {
Route::get('hello/:name', 'index/hello'); Route::get('blog/:name', 'index/blog');
Route::get('hello/:id', 'index/hello'); Route::get('blog/:id', 'index/blog');
Config::set('pathinfo_depr', '/'); Config::set('pathinfo_depr', '/');
$this->assertEquals('/hello/thinkphp', Url::build('index/hello?name=thinkphp')); $this->assertEquals('/blog/thinkphp', Url::build('index/blog?name=thinkphp'));
$this->assertEquals('/hello/thinkphp.html', Url::build('index/hello', 'name=thinkphp', 'html')); $this->assertEquals('/blog/thinkphp.html', Url::build('index/blog', 'name=thinkphp', 'html'));
$this->assertEquals('/hello/10', Url::build('index/hello?id=10')); $this->assertEquals('/blog/10', Url::build('index/blog?id=10'));
$this->assertEquals('/hello/10.html', Url::build('index/hello', 'id=10', 'html')); $this->assertEquals('/blog/10.html', Url::build('index/blog', 'id=10', 'html'));
Route::get('item-<item><id?>', 'good/item', [], ['item' => '\w+', 'id' => '\d+']); Route::get('item-<name><id?>', 'blog/item', [], ['name' => '\w+', 'id' => '\d+']);
$this->assertEquals('/item-thinkphp', Url::build('good/item?item=thinkphp')); $this->assertEquals('/item-thinkphp', Url::build('blog/item?name=thinkphp'));
$this->assertEquals('/item-thinkphp2016', Url::build('good/item?item=thinkphp&id=2016')); $this->assertEquals('/item-thinkphp2016', Url::build('blog/item?name=thinkphp&id=2016'));
} }
public function testBuildController() public function testBuildController()