public static function domain($domain, $rule = '', $option = [], $pattern = []) { if (is_array($domain)) { foreach ($domain as $key => $item) { self::domain($key, $item, $option, $pattern); } } elseif ($rule instanceof \Closure) { // 执行闭包 self::setDomain($domain); call_user_func_array($rule, []); self::setDomain(null); } elseif (is_array($rule)) { self::setDomain($domain); self::group('', function () use($rule) { // 动态注册域名的路由规则 self::registerRules($rule); }, $option, $pattern); self::setDomain(null); } else { self::$rules['domain'][$domain]['[bind]'] = [$rule, $option, $pattern]; } }
public function testBuildDomain() { Config::set('url_domain_deploy', true); Route::domain('subdomain.thinkphp.cn', 'admin'); $this->assertEquals('http://subdomain.thinkphp.cn/blog/10.shtml', Url::build('/blog/10')); Route::domain('subdomain.thinkphp.cn', ['hello/:name' => 'index/hello']); $this->assertEquals('http://subdomain.thinkphp.cn/hello/thinkphp.shtml', Url::build('index/hello?name=thinkphp')); }