根据指定的url地址获取其url路径

通过前端访问后台执行操作,如 $.ajax,$.post,$.get等等所有包含url: "地址" 属性都可以调用该方法,方法如下:

前端通过$.post 进行调用后台操作:

<script>
$(function(){
$.post(
   '<?php echo $this->getUrl('index/index/loginValidate'); ?>',
   $('#loginFm').serialize(),
   function(result){
      if(result.error){
         $('.messages').html('<p class="error-msg">'+result.msg.join('<br>')+'</p>');
      }else{
         $('.messages').html('<p class="success-msg">登录成功,请稍候...</p>');
         top.location.replace(window.location.href);
      }
   },
   'json'
);
});
</script>

后端php方法

public static function getUrl($path = '', $sep = false)
	{
		global $_CONFIG;
		$path = trim($path, '/');
		$url = '';
		if ($_CONFIG['HttpPath']) {
			$url = APP_HTTP . $path . ($sep ? '?' : '');
		} else {
			$param = explode('/', $path);
			$count = count($param);
			$url = APP_HTTP . 'index.php';
			$start = 2;
			if ($_CONFIG['GroupStart']) {
				$start = 3;
				$url .= '?' . $_CONFIG['UrlGroupName'] . '=' . (isset($param[0]) ? $param[0] : 'index');
				$url .= '&' . $_CONFIG['UrlControllerName'] . '=' . (isset($param[1]) ? $param[1] : 'index');
				$url .= '&' . $_CONFIG['UrlActionName'] . '=' . (isset($param[2]) ? $param[2] : 'index');
			} else {
				$url .= '?' . $_CONFIG['UrlControllerName'] . '=' . (isset($param[0]) ? $param[0] : 'index');
				$url .= '&' . $_CONFIG['UrlActionName'] . '=' . (isset($param[1]) ? $param[1] : 'index');
			}
			if ($count > $start) {
				for ($i = $start; $i < $count; $i++) {
					$url .= '&' . (isset($param[$i]) ? $param[$i] : '');
					$url .= '=' . (isset($param[++$i]) ? $param[$i] : '');
					$i++;
				}
			}
			$url .= $sep ? '&' : '';
		}
		return $url;
	}

config.php 配置

$_CONFIG['HttpPath']      = true;			// 是否开启
$_CONFIG['GroupStart']    = true;			// 是否开启 分组模式
$_CONFIG['urlGroup']      = 'g';			// 自定义分组名称 例如: index.php?g=index
$_CONFIG['urlController'] = 'c';			// 自定义控制器名称 例如: index.php?g=index&c=index
$_CONFIG['urlAction']     = 'a';			// 自定义方法名称 例如: index.php?g=index&c=index&a=index

 


版权声明:本文为qq_36627117原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。