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

@@ -243,7 +243,7 @@ class App
if ($config['app_multi_module']) {
// 多模块部署
$module = strip_tags(strtolower($result[0] ?: $config['default_module']));
$bind = Route::bind('module');
$bind = Route::getBind('module');
$available = false;
if ($bind) {
// 绑定模块
@@ -364,7 +364,6 @@ class App
return self::$init;
}
/**
* 初始化应用或模块
* @access public

View File

@@ -123,19 +123,26 @@ class Route
}
/**
* 设置和读取路由绑定
* 设置路由绑定
* @access public
* @param string $type 请求类型
* @param mixed $bind 绑定信息
* @param string $type 绑定类型 默认为module
* @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];
}
/**
* 读取路由绑定
* @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绑定
$type = Route::bind('type');
$type = Route::getBind('type');
if ($type) {
$bind = Route::bind($type);
$bind = Route::getBind($type);
if (0 === strpos($url, $bind)) {
$url = substr($url, strlen($bind) + 1);
}

View File

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

View File

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