CUrl.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fec\helpers;
  10. use Yii;
  11. /**
  12. * @author Terry Zhao <2358269014@qq.com>
  13. * @since 1.0
  14. */
  15. class CUrl
  16. {
  17. public static $_baseHttpUrl;
  18. public static $_baseHttpsUrl;
  19. # 1.获取首页地址。
  20. public static function getHomeUrl(){
  21. return Yii::$app->getHomeUrl();
  22. //return Yii::$app->getBaseUrl(true);
  23. }
  24. # 2. 获取首页地址。同上
  25. public static function getBaseUrl(){
  26. if(!self::$_baseHttpsUrl){
  27. if(\Yii::$app->urlManager->enablePrettyUrl && (!\Yii::$app->urlManager->showScriptName)){
  28. self::$_baseHttpsUrl = self::getHomeUrl().'/index.php';
  29. }
  30. self::$_baseHttpsUrl = self::getHomeUrl();
  31. }
  32. return self::$_baseHttpsUrl;
  33. }
  34. # 3.立即跳转 和 yii2的跳转还是不同
  35. public static function redirect($url,$isHttps=false){
  36. if($url){
  37. if(substr($url,0,4) != "http"){
  38. $url = self::getUrl($url,[],$isHttps);
  39. }
  40. header("Location: $url");
  41. exit;
  42. }
  43. }
  44. #5. 通过url path 和参数 得到当前网站下的完整url路径。
  45. public static function getUrl($url_path,$params=array(),$isHttps=false){
  46. $url_path = trim($url_path,'/');
  47. $url = self::getBaseUrl(). '/' .$url_path;
  48. if(!empty($params) && is_array($params)){
  49. $arr = [];
  50. foreach($params as $k=>$v){
  51. $arr[] = $k."=".$v;
  52. }
  53. return $url.'?'.implode('&',$arr);
  54. }
  55. return $url;
  56. }
  57. # 6.得到当前的完整url
  58. public static function getCurrentUrl(){
  59. //$s = self::getHomeUrl();
  60. //return $s.$_SERVER["REQUEST_URI"];
  61. return \yii\helpers\Url::current();
  62. }
  63. # 7.得到当前的完整url no param
  64. public static function getCurrentUrlNoParam(){
  65. $url = self::getCurrentUrl();
  66. if(strstr($url,"#")){
  67. $url = substr($url,0,strpos($url,"#"));
  68. }
  69. if(strstr($url,"?")){
  70. $url = substr($url,0,strpos($url,"?"));
  71. }
  72. return $url;
  73. }
  74. # 8、得到url key ,譬如 http://www.x.com/ss/dd/aa?aaaa=ddddd 返回 /ss/dd/aa
  75. public static function getUrlKey(){
  76. return Yii::$app->request->getPathInfo();
  77. }
  78. # 9.得到url ,譬如 http://www.x.com/ss/dd/aa?aaaa=ddddd 返回 /ss/dd/aa?aaaa=ddddd
  79. public static function getUrlKeyWithParam(){
  80. return Yii::$app->getRequest()->url;
  81. }
  82. }