Route类的bind方法参数顺序调整 增加getBind方法用于获取绑定信息

This commit is contained in:
thinkphp
2016-06-24 12:33:07 +08:00
parent 1a8bbf6725
commit 87ad5e95bb
5 changed files with 67 additions and 61 deletions

View File

@@ -108,7 +108,7 @@ class App
break; break;
case 'module': case 'module':
// 模块/控制器/操作 // 模块/控制器/操作
$data = self::module($dispatch['module'], $config, isset($dispatch['convert']) ? $dispatch['convert'] : null ); $data = self::module($dispatch['module'], $config, isset($dispatch['convert']) ? $dispatch['convert'] : null);
break; break;
case 'controller': case 'controller':
// 执行控制器操作 // 执行控制器操作
@@ -140,7 +140,7 @@ class App
// 输出数据到客户端 // 输出数据到客户端
if ($data instanceof Response) { if ($data instanceof Response) {
return $data; return $data;
} elseif(!is_null($data)) { } elseif (!is_null($data)) {
// 默认自动识别响应输出类型 // 默认自动识别响应输出类型
$isAjax = $request->isAjax(); $isAjax = $request->isAjax();
$type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type'); $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
@@ -222,7 +222,7 @@ class App
} }
} }
// 全局过滤 // 全局过滤
array_walk_recursive($args, [Request::instance(),'filterExp']); array_walk_recursive($args, [Request::instance(), 'filterExp']);
} }
return $args; return $args;
} }
@@ -243,7 +243,7 @@ class App
if ($config['app_multi_module']) { if ($config['app_multi_module']) {
// 多模块部署 // 多模块部署
$module = strip_tags(strtolower($result[0] ?: $config['default_module'])); $module = strip_tags(strtolower($result[0] ?: $config['default_module']));
$bind = Route::bind('module'); $bind = Route::getBind('module');
$available = false; $available = false;
if ($bind) { if ($bind) {
// 绑定模块 // 绑定模块
@@ -364,7 +364,6 @@ class App
return self::$init; return self::$init;
} }
/** /**
* 初始化应用或模块 * 初始化应用或模块
* @access public * @access public
@@ -432,7 +431,7 @@ class App
{ {
// 检测URL禁用后缀 // 检测URL禁用后缀
if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', $request->pathinfo())) { if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', $request->pathinfo())) {
throw new Exception('url suffix deny:'.$request->ext()); throw new Exception('url suffix deny:' . $request->ext());
} }
$path = $request->path(); $path = $request->path();

View File

@@ -123,19 +123,26 @@ class Route
} }
/** /**
* 设置和读取路由绑定 * 设置路由绑定
* @access public * @access public
* @param string $type 请求类型
* @param mixed $bind 绑定信息 * @param mixed $bind 绑定信息
* @param string $type 绑定类型 默认为module
* @return mixed * @return mixed
*/ */
public static function bind($type, $bind = '') public static function bind($bind, $type = 'module')
{ {
if ('' == $bind) {
return isset(self::$bind[$type]) ? self::$bind[$type] : null;
} else {
self::$bind = ['type' => $type, $type => $bind]; self::$bind = ['type' => $type, $type => $bind];
} }
/**
* 读取路由绑定
* @access public
* @param string $type 绑定类型
* @return mixed
*/
public static function getBind($type)
{
return isset(self::$bind[$type]) ? self::$bind[$type] : null;
} }
/** /**

View File

@@ -78,9 +78,9 @@ class Url
} }
// 检测URL绑定 // 检测URL绑定
$type = Route::bind('type'); $type = Route::getBind('type');
if ($type) { if ($type) {
$bind = Route::bind($type); $bind = Route::getBind($type);
if (0 === strpos($url, $bind)) { if (0 === strpos($url, $bind)) {
$url = substr($url, strlen($bind) + 1); $url = substr($url, strlen($bind) + 1);
} }

View File

@@ -506,7 +506,7 @@ class Cx extends Taglib
// 文件方式导入 // 文件方式导入
$array = explode(',', $file); $array = explode(',', $file);
foreach ($array as $val) { foreach ($array as $val) {
$type = $reset = strtolower(substr(strrchr($val, '.'), 1)); $type = strtolower(substr(strrchr($val, '.'), 1));
switch ($type) { switch ($type) {
case 'js': case 'js':
$parseStr .= '<script type="text/javascript" src="' . $val . '"></script>'; $parseStr .= '<script type="text/javascript" src="' . $val . '"></script>';

View File

@@ -16,16 +16,16 @@
namespace tests\thinkphp\library\think; namespace tests\thinkphp\library\think;
use think\Config;
use think\Request; use think\Request;
use think\Route; use think\Route;
use think\Config;
class routeTest extends \PHPUnit_Framework_TestCase class routeTest extends \PHPUnit_Framework_TestCase
{ {
protected function setUp() protected function setUp()
{ {
Config::set('app_multi_module',true); Config::set('app_multi_module', true);
} }
public function testRegister() public function testRegister()
@@ -183,7 +183,7 @@ class routeTest extends \PHPUnit_Framework_TestCase
public function testBind() public function testBind()
{ {
$request = Request::instance(); $request = Request::instance();
Route::bind('module', 'index/blog'); Route::bind('index/blog');
$result = Route::parseUrl('read/10'); $result = Route::parseUrl('read/10');
$this->assertEquals(['index', 'blog', 'read'], $result['module']); $this->assertEquals(['index', 'blog', 'read'], $result['module']);
@@ -191,10 +191,10 @@ class routeTest extends \PHPUnit_Framework_TestCase
$result = Route::check($request, '10'); $result = Route::check($request, '10');
$this->assertEquals(['index', 'blog', 'read'], $result['module']); $this->assertEquals(['index', 'blog', 'read'], $result['module']);
Route::bind('namespace', '\app\index\controller'); Route::bind('\app\index\controller', 'namespace');
$this->assertEquals(['type' => 'method', 'method' => ['\app\index\controller\blog', 'read'], 'params' => []], Route::check($request, 'blog/read')); $this->assertEquals(['type' => 'method', 'method' => ['\app\index\controller\blog', 'read'], 'params' => []], Route::check($request, 'blog/read'));
Route::bind('class', '\app\index\controller\blog'); Route::bind('\app\index\controller\blog', 'class');
$this->assertEquals(['type' => 'method', 'method' => ['\app\index\controller\blog', 'read'], 'params' => []], Route::check($request, 'read')); $this->assertEquals(['type' => 'method', 'method' => ['\app\index\controller\blog', 'read'], 'params' => []], Route::check($request, 'read'));
} }
@@ -204,24 +204,24 @@ class routeTest extends \PHPUnit_Framework_TestCase
Route::domain('subdomain.thinkphp.cn', 'sub?abc=test&status=1'); Route::domain('subdomain.thinkphp.cn', 'sub?abc=test&status=1');
Route::checkDomain($request); Route::checkDomain($request);
$this->assertEquals('sub?abc=test&status=1', Route::domain('subdomain.thinkphp.cn')); $this->assertEquals('sub?abc=test&status=1', Route::domain('subdomain.thinkphp.cn'));
$this->assertEquals('sub', Route::bind('module')); $this->assertEquals('sub', Route::getbind('module'));
$this->assertEquals('test', $_GET['abc']); $this->assertEquals('test', $_GET['abc']);
$this->assertEquals(1, $_GET['status']); $this->assertEquals(1, $_GET['status']);
Route::domain('subdomain.thinkphp.cn', function () {return ['type' => 'module', 'module' => 'sub2'];}); Route::domain('subdomain.thinkphp.cn', function () {return ['type' => 'module', 'module' => 'sub2'];});
Route::checkDomain($request); Route::checkDomain($request);
$this->assertEquals('sub2', Route::bind('module')); $this->assertEquals('sub2', Route::getbind('module'));
Route::domain('subdomain.thinkphp.cn', '\app\index\controller'); Route::domain('subdomain.thinkphp.cn', '\app\index\controller');
Route::checkDomain($request); Route::checkDomain($request);
$this->assertEquals('\app\index\controller', Route::bind('namespace')); $this->assertEquals('\app\index\controller', Route::getbind('namespace'));
Route::domain('subdomain.thinkphp.cn', '@\app\index\controller\blog'); Route::domain('subdomain.thinkphp.cn', '@\app\index\controller\blog');
Route::checkDomain($request); Route::checkDomain($request);
$this->assertEquals('\app\index\controller\blog', Route::bind('class')); $this->assertEquals('\app\index\controller\blog', Route::getbind('class'));
Route::domain('subdomain.thinkphp.cn', '[sub3]'); Route::domain('subdomain.thinkphp.cn', '[sub3]');
Route::checkDomain($request); Route::checkDomain($request);
$this->assertEquals('sub3', Route::bind('group')); $this->assertEquals('sub3', Route::getbind('group'));
} }
} }