|
- <?php
-
-
- namespace yii\helpers;
-
- use Yii;
- use yii\base\InvalidParamException;
-
-
- class BaseUrl
- {
-
-
- public static $urlManager;
-
-
-
-
- public static function toRoute($route, $scheme = false)
- {
- $route = (array) $route;
- $route[0] = static::normalizeRoute($route[0]);
-
- if ($scheme) {
- return static::getUrlManager()->createAbsoluteUrl($route, is_string($scheme) ? $scheme : null);
- } else {
- return static::getUrlManager()->createUrl($route);
- }
- }
-
-
-
- protected static function normalizeRoute($route)
- {
- $route = Yii::getAlias((string) $route);
- if (strncmp($route, '/', 1) === 0) {
-
- return ltrim($route, '/');
- }
-
-
- if (Yii::$app->controller === null) {
- throw new InvalidParamException("Unable to resolve the relative route: $route. No active controller is available.");
- }
-
- if (strpos($route, '/') === false) {
-
- return $route === '' ? Yii::$app->controller->getRoute() : Yii::$app->controller->getUniqueId() . '/' . $route;
- } else {
-
- return ltrim(Yii::$app->controller->module->getUniqueId() . '/' . $route, '/');
- }
- }
-
-
-
- public static function to($url = '', $scheme = false)
- {
- if (is_array($url)) {
- return static::toRoute($url, $scheme);
- }
-
- $url = Yii::getAlias($url);
- if ($url === '') {
- $url = Yii::$app->getRequest()->getUrl();
- }
-
- if (!$scheme) {
- return $url;
- }
-
- if (strncmp($url, '//', 2) === 0) {
-
- return is_string($scheme) ? "$scheme:$url" : $url;
- }
-
- if (($pos = strpos($url, ':')) === false || !ctype_alpha(substr($url, 0, $pos))) {
-
- $url = static::getUrlManager()->getHostInfo() . '/' . ltrim($url, '/');
- }
-
- if (is_string($scheme) && ($pos = strpos($url, ':')) !== false) {
-
- $url = $scheme . substr($url, $pos);
- }
-
- return $url;
- }
-
-
-
- public static function base($scheme = false)
- {
- $url = static::getUrlManager()->getBaseUrl();
- if ($scheme) {
- $url = static::getUrlManager()->getHostInfo() . $url;
- if (is_string($scheme) && ($pos = strpos($url, '://')) !== false) {
- $url = $scheme . substr($url, $pos);
- }
- }
- return $url;
- }
-
-
-
- public static function remember($url = '', $name = null)
- {
- $url = static::to($url);
-
- if ($name === null) {
- Yii::$app->getUser()->setReturnUrl($url);
- } else {
- Yii::$app->getSession()->set($name, $url);
- }
- }
-
-
-
- public static function previous($name = null)
- {
- if ($name === null) {
- return Yii::$app->getUser()->getReturnUrl();
- } else {
- return Yii::$app->getSession()->get($name);
- }
- }
-
-
-
- public static function canonical()
- {
- $params = Yii::$app->controller->actionParams;
- $params[0] = Yii::$app->controller->getRoute();
-
- return static::getUrlManager()->createAbsoluteUrl($params);
- }
-
-
-
- public static function home($scheme = false)
- {
- $url = Yii::$app->getHomeUrl();
-
- if ($scheme) {
- $url = static::getUrlManager()->getHostInfo() . $url;
- if (is_string($scheme) && ($pos = strpos($url, '://')) !== false) {
- $url = $scheme . substr($url, $pos);
- }
- }
-
- return $url;
- }
-
-
-
- public static function isRelative($url)
- {
- return strncmp($url, '//', 2) && strpos($url, '://') === false;
- }
-
-
-
- public static function current(array $params = [], $scheme = false)
- {
- $currentParams = Yii::$app->getRequest()->getQueryParams();
- $currentParams[0] = '/' . Yii::$app->controller->getRoute();
- $route = ArrayHelper::merge($currentParams, $params);
- return static::toRoute($route, $scheme);
- }
-
-
-
- protected static function getUrlManager()
- {
- return static::$urlManager ?: Yii::$app->getUrlManager();
- }
- }
|