rule() public static method

注册路由规则
public static rule ( string $rule, string $route = '', string $type = '*', array $option = [], array $pattern = [] ) : void
$rule string 路由规则
$route string 路由地址
$type string 请求类型
$option array 路由参数
$pattern array 变量规则
return void
Example #1
0
 public function testCheckRouteGroup()
 {
     $request = Request::instance();
     Route::pattern(['id' => '\\d+']);
     Route::pattern('name', '\\w{6,25}');
     Route::group('group', [':id' => 'index/hello', ':name' => 'index/say']);
     $this->assertEquals(false, Route::check($request, 'empty/think'));
     $result = Route::check($request, 'group/think');
     $this->assertEquals(false, $result['module']);
     $result = Route::check($request, 'group/10');
     $this->assertEquals([null, 'index', 'hello'], $result['module']);
     $result = Route::check($request, 'group/thinkphp');
     $this->assertEquals([null, 'index', 'say'], $result['module']);
     Route::group('group2', function () {
         Route::group('group3', [':id' => 'index/hello', ':name' => 'index/say']);
         Route::rule(':name', 'index/hello');
         Route::auto('index');
     });
     $result = Route::check($request, 'group2/thinkphp');
     $this->assertEquals([null, 'index', 'hello'], $result['module']);
     $result = Route::check($request, 'group2/think');
     $this->assertEquals(['index', 'group2', 'think'], $result['module']);
     $result = Route::check($request, 'group2/group3/thinkphp');
     $this->assertEquals([null, 'index', 'say'], $result['module']);
     Route::group('group4', function () {
         Route::group('group3', [':id' => 'index/hello', ':name' => 'index/say']);
         Route::rule(':name', 'index/hello');
         Route::miss('index/__miss__');
     });
     $result = Route::check($request, 'group4/thinkphp');
     $this->assertEquals([null, 'index', 'hello'], $result['module']);
     $result = Route::check($request, 'group4/think');
     $this->assertEquals([null, 'index', '__miss__'], $result['module']);
     Route::group(['prefix' => 'prefix/'], function () {
         Route::rule('hello4/:name', 'hello');
     });
     Route::group(['prefix' => 'prefix/'], ['hello4/:name' => 'hello']);
     $result = Route::check($request, 'hello4/thinkphp');
     $this->assertEquals([null, 'prefix', 'hello'], $result['module']);
     Route::group('group5', [[':name', 'hello', ['method' => 'GET|POST']], ':id' => 'hello'], ['prefix' => 'prefix/']);
     $result = Route::check($request, 'group5/thinkphp');
     $this->assertEquals([null, 'prefix', 'hello'], $result['module']);
 }
Example #2
0
/**
 * 路由注册
 * @param string $rule 路由规则
 * @param mixed $route 路由地址
 * @param sting $type 请求类型
 * @param array $option 路由参数
 * @param array $pattern 变量规则
 * @return void
 */
function route($rule = '', $route = [], $type = '*', $option = [], $pattern = [])
{
    Route::rule($rule, $route, $type, $option, $pattern);
}