改进Url类

This commit is contained in:
thinkphp
2015-12-11 09:16:42 +08:00
parent ea9041b858
commit b34d2f3a1b
3 changed files with 92 additions and 65 deletions

View File

@@ -57,6 +57,8 @@ return [
'base_url' => $_SERVER["SCRIPT_NAME"],
// URL伪静态后缀
'url_html_suffix' => '.html',
// URL普通方式参数 用于自动生成
'url_common_param' => false,
// url变量绑定
'url_params_bind' => true,
// URL变量绑定的类型 0 按变量名绑定 1 按变量顺序绑定
@@ -69,6 +71,10 @@ return [
'url_route_must' => false,
// URL模块映射
'url_module_map' => [],
// 域名部署
'url_domain_deploy' => false,
// 域名部署规则
'url_domain_rules' => [],
// +----------------------------------------------------------------------
// | 视图及模板设置

View File

@@ -233,8 +233,8 @@ class App
}
// 检测域名部署
if (!IS_CLI && !empty($config['domain_deploy'])) {
if ($match = Route::checkDomain($config['domain_rules'])) {
if (!IS_CLI && !empty($config['url_domain_deploy'])) {
if ($match = Route::checkDomain($config['url_domain_rules'])) {
(!defined('BIND_MODULE') && !empty($match[0])) && define('BIND_MODULE', $match[0]);
(!defined('BIND_CONTROLLER') && !empty($match[1])) && define('BIND_CONTROLLER', $match[1]);
(!defined('BIND_ACTION') && !empty($match[2])) && define('BIND_ACTION', $match[2]);

View File

@@ -24,8 +24,87 @@ class Url
*/
public static function build($url = '', $vars = '', $suffix = true, $domain = false)
{
$config = Config::get();
// 解析URL
// 解析URL和参数
list($url, $vars, $domain, $anchor) = self::parseUrl($url, $vars, $domain);
// URL组装
$depr = Config::get('pathinfo_depr');
if ($url) {
if (0 === strpos($url, '/')) {
// 定义路由
$route = true;
$url = substr($url, 1);
if ('/' != $depr) {
$url = str_replace('/', $depr, $url);
}
} else {
if ('/' != $depr) {
// 安全替换
$url = str_replace('/', $depr, $url);
}
// 解析模块、控制器和操作
$url = trim($url, $depr);
$path = explode($depr, $url);
$var = [];
$var['action'] = !empty($path) ? array_pop($path) : ACTION_NAME;
if (!defined('BIND_CONTROLLER')) {
$var['controller'] = !empty($path) ? array_pop($path) : CONTROLLER_NAME;
}
if (!defined('BIND_MODULE')) {
$var['module'] = !empty($path) ? array_pop($path) : MODULE_NAME;
}
}
}
// URL组装
$url = Config::get('base_url') . '/' . (isset($route) ? rtrim($url, $depr) : implode($depr, array_reverse($var)));
// URL后缀
$suffix = self::parseSuffix($suffix);
// 参数组装
if (!empty($vars)) {
// 添加参数
if (Config::get('url_common_param')) {
$vars = urldecode(http_build_query($vars));
$url .= $suffix . '?' . $vars;
} else {
foreach ($vars as $var => $val) {
if ('' !== trim($val)) {
$url .= $depr . $var . $depr . urlencode($val);
}
}
$url .= $suffix;
}
}
if (isset($anchor)) {
$url .= '#' . $anchor;
}
if ($domain) {
$url = (self::isSsl() ? 'https://' : 'http://') . $domain . $url;
}
return $url;
}
// 解析URL后缀
protected static function parseSuffix($suffix)
{
if ($suffix) {
$suffix = true === $suffix ? Config::get('url_html_suffix') : $suffix;
if ($pos = strpos($suffix, '|')) {
$suffix = substr($suffix, 0, $pos);
}
}
return $suffix;
}
// 根据路由名称和参数生成URL地址
public static function route($name, $params)
{
}
// 解析URL和参数 域名
protected static function parseUrl($url, $vars, $domain)
{
$info = parse_url($url);
$url = !empty($info['path']) ? $info['path'] : ACTION_NAME;
if (isset($info['fragment'])) {
@@ -48,11 +127,11 @@ class Url
$domain = $host . (strpos($host, '.') ? '' : strstr($_SERVER['HTTP_HOST'], '.'));
} elseif (true === $domain) {
$domain = $_SERVER['HTTP_HOST'];
if ($config['domain_deplay']) {
if (Config::get('url_domain_deplay')) {
// 开启子域名部署
$domain = 'localhost' == $domain ? 'localhost' : 'www' . strstr($_SERVER['HTTP_HOST'], '.');
// '子域名'=>array('项目[/分组]');
foreach ($config['domain_rules'] as $key => $rule) {
foreach (Config::get('url_domain_rules') as $key => $rule) {
if (false === strpos($key, '*') && 0 === strpos($url, $rule[0])) {
$domain = $key . strstr($domain, '.'); // 生成对应子域名
$url = substr_replace($url, '', 0, strlen($rule[0]));
@@ -61,7 +140,6 @@ class Url
}
}
}
// 解析参数
if (is_string($vars)) {
// aaa=1&bbb=2 转换成数组
@@ -74,64 +152,7 @@ class Url
parse_str($info['query'], $params);
$vars = array_merge($params, $vars);
}
// URL组装
$depr = $config['pathinfo_depr'];
if ($url) {
if (0 === strpos($url, '/')) {
// 定义路由
$route = true;
$url = substr($url, 1);
if ('/' != $depr) {
$url = str_replace('/', $depr, $url);
}
} else {
if ('/' != $depr) {
// 安全替换
$url = str_replace('/', $depr, $url);
}
// 解析模块、控制器和操作
$url = trim($url, $depr);
$path = explode($depr, $url);
$var = [];
$var[VAR_ACTION] = !empty($path) ? array_pop($path) : ACTION_NAME;
if (!defined('BIND_CONTROLLER')) {
$var[VAR_CONTROLLER] = !empty($path) ? array_pop($path) : CONTROLLER_NAME;
}
if (!defined('BIND_MODULE')) {
$var[VAR_MODULE] = !empty($path) ? array_pop($path) : MODULE_NAME;
}
}
}
$url = $config['base_url'] . '/' . (isset($route) ? rtrim($url, $depr) : implode($depr, array_reverse($var)));
if (!empty($vars)) {
// 添加参数
if ($config['url_common_param']) {
$vars = urldecode(http_build_query($vars));
$url .= '?' . $vars;
} else {
$url .= $depr . implode($depr, $vars);
}
}
if ($suffix) {
$suffix = true === $suffix ? $config['url_html_suffix'] : $suffix;
if ($pos = strpos($suffix, '|')) {
$suffix = substr($suffix, 0, $pos);
}
if ($suffix && '/' != substr($url, -1)) {
$url .= '.' . ltrim($suffix, '.');
}
}
if (isset($anchor)) {
$url .= '#' . $anchor;
}
if ($domain) {
$url = (self::isSsl() ? 'https://' : 'http://') . $domain . $url;
}
return $url;
return [$url, $vars, $domain, $anchor];
}
/**