URL生成 支持路由反射
public static build ( string $url = '', string | array $vars = '', string | boolean $suffix = true, boolean | string $domain = false ) : string | ||
$url | string | 路由地址 |
$vars | string | array | 参数(支持数组和字符串)a=val&b=val2... ['a'=>'val1', 'b'=>'val2'] |
$suffix | string | boolean | 伪静态后缀,默认为true表示获取配置值 |
$domain | boolean | string | 是否显示域名 或者直接传入域名 |
return | string |
/** * 操作错误跳转的快捷方法 * @access protected * @param mixed $msg 提示信息 * @param string $url 跳转的URL地址 * @param mixed $data 返回的数据 * @param integer $wait 跳转等待时间 * @return void */ protected function error($msg = '', $url = null, $data = '', $wait = 3) { $code = 0; if (is_numeric($msg)) { $code = $msg; $msg = ''; } if (is_null($url)) { $url = 'javascript:history.back(-1);'; } else { $url = preg_match('/^(https?:|\\/)/', $url) ? $url : Url::build($url); } $result = ['code' => $code, 'msg' => $msg, 'data' => $data, 'url' => $url, 'wait' => $wait]; $type = $this->getResponseType(); if ('html' == strtolower($type)) { $result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))->fetch(Config::get('dispatch_error_tmpl'), $result); } $response = Response::create($result, $type); throw new HttpResponseException($response); }
function U($url, $vars = '', $suffix = true, $domain = false) { return \think\Url::build($url, $vars, $suffix, $domain); }
/** * Url生成 * @param string $url 路由地址 * @param string|array $vars 变量 * @param bool|string $suffix 生成的URL后缀 * @param bool|string $domain 域名 * @return string */ function url($url = '', $vars = '', $suffix = true, $domain = false) { return Url::build($url, $vars, $suffix, $domain); }
public function testRoot() { Config::set('url_domain_deploy', false); Config::set('url_common_param', false); Url::root('/index.php'); Route::get('blog/:id', 'index/blog/read'); Config::set('url_html_suffix', 'shtml'); $this->assertEquals('/index.php/blog/10/name/thinkphp.shtml', Url::build('index/blog/read?id=10&name=thinkphp')); }
/** * URL重定向 * @access protected * @param string $url 跳转的URL表达式 * @param array $params 其它URL参数 * @return void */ public static function redirect($url, $params = []) { $url = Url::build($url, $params); header('Location: ' . $url); }
/** * 获取跳转地址 * @return string */ public function getTargetUrl() { return strpos($this->data, '://') || 0 === strpos($this->data, '/') ? $this->data : Url::build($this->data, $this->params); }
/** * 获取跳转地址 * @return string */ public function getTargetUrl() { return preg_match('/^(https?:|\\/)/', $this->data) ? $this->data : Url::build($this->data, $this->params); }
/** * URL重定向 * @access public * @param string $url 跳转的URL表达式 * @param array|int $params 其它URL参数或http code * @return void */ public function redirect($url, $params = []) { $http_response_code = 301; if (is_int($params) && in_array($params, [301, 302])) { $http_response_code = $params; $params = []; } $url = preg_match('/^(https?:|\\/)/', $url) ? $url : Url::build($url, $params); header('Location: ' . $url, true, $http_response_code); }
private function ResponseCreate($code, $msg, $data, $url, $wait, $header) { if (!empty($url)) { $url = preg_match('/^(https?:|\\/)/', $url) ? $url : Url::build($url); } $result = ['code' => $code, 'msg' => $msg, 'data' => $data, 'url' => $url, 'wait' => $wait]; $type = $this->getResponseType(); if ('html' == strtolower($type)) { $result = $this->fetch(Config::get('dispatch_success_tmpl'), $result); } return Response::create($result, $type)->header($header); }
public function testBuildAnchor() { Route::get('blog/:id', 'index/blog'); Config::set('url_html_suffix', 'shtml'); $this->assertEquals('/blog/10.shtml#detail', Url::build('/blog/10#detail')); }
public function testUrl() { $template = new template(); $content = <<<EOF {url link="Index/index" /} EOF; $template->display($content); $this->expectOutputString(\think\Url::build('Index/index')); }