Commit cd1b3d32 by Qiang Xue

refactored url creation shortcut method.

parent 8724d803
...@@ -78,7 +78,7 @@ class Application extends Module ...@@ -78,7 +78,7 @@ class Application extends Module
*/ */
public $preload = array(); public $preload = array();
/** /**
* @var Controller the currently active controller instance * @var \yii\web\Controller|\yii\console\Controller the currently active controller instance
*/ */
public $controller; public $controller;
/** /**
...@@ -355,7 +355,7 @@ class Application extends Module ...@@ -355,7 +355,7 @@ class Application extends Module
/** /**
* Returns the request component. * Returns the request component.
* @return Request the request component * @return \yii\web\Request|\yii\console\Request the request component
*/ */
public function getRequest() public function getRequest()
{ {
......
...@@ -949,11 +949,10 @@ class Html ...@@ -949,11 +949,10 @@ class Html
* If the input parameter * If the input parameter
* *
* - is an empty string: the currently requested URL will be returned; * - is an empty string: the currently requested URL will be returned;
* - is a non-empty string: it will be processed by [[Yii::getAlias()]] which, if the string is an alias, * - is a non-empty string: it will be processed by [[Yii::getAlias()]] and returned;
* will be resolved into a URL;
* - is an array: the first array element is considered a route, while the rest of the name-value * - is an array: the first array element is considered a route, while the rest of the name-value
* pairs are considered as the parameters to be used for URL creation using [[\yii\base\Application::createUrl()]]. * pairs are treated as the parameters to be used for URL creation using [[\yii\web\Controller::createUrl()]].
* Here are some examples: `array('post/index', 'page' => 2)`, `array('index')`. * For example: `array('post/index', 'page' => 2)`, `array('index')`.
* *
* @param array|string $url the parameter to be used to generate a valid URL * @param array|string $url the parameter to be used to generate a valid URL
* @return string the normalized URL * @return string the normalized URL
...@@ -963,7 +962,13 @@ class Html ...@@ -963,7 +962,13 @@ class Html
{ {
if (is_array($url)) { if (is_array($url)) {
if (isset($url[0])) { if (isset($url[0])) {
return Yii::$app->createUrl($url[0], array_splice($url, 1)); $route = $url[0];
$params = array_splice($url, 1);
if (Yii::$app->controller !== null) {
return Yii::$app->controller->createUrl($route, $params);
} else {
return Yii::$app->getUrlManager()->createUrl($route, $params);
}
} else { } else {
throw new InvalidParamException('The array specifying a URL must contain at least one element.'); throw new InvalidParamException('The array specifying a URL must contain at least one element.');
} }
......
...@@ -78,51 +78,6 @@ class Application extends \yii\base\Application ...@@ -78,51 +78,6 @@ class Application extends \yii\base\Application
} }
/** /**
* Creates a URL using the given route and parameters.
*
* This method first normalizes the given route by converting a relative route into an absolute one.
* A relative route is a route without a leading slash. It is considered to be relative to the currently
* requested route. If the route is an empty string, it stands for the route of the currently active
* [[controller]]. Otherwise, the [[Controller::uniqueId]] will be prepended to the route.
*
* After normalizing the route, this method calls [[\yii\web\UrlManager::createUrl()]]
* to create a relative URL.
*
* @param string $route the route. This can be either an absolute or a relative route.
* @param array $params the parameters (name-value pairs) to be included in the generated URL
* @return string the created URL
* @throws InvalidParamException if a relative route is given and there is no active controller.
* @see createAbsoluteUrl
*/
public function createUrl($route, $params = array())
{
if (strncmp($route, '/', 1) !== 0) {
// a relative route
if ($this->controller !== null) {
$route = $route === '' ? $this->controller->route : $this->controller->uniqueId . '/' . $route;
} else {
throw new InvalidParamException('Relative route cannot be handled because there is no active controller.');
}
}
return $this->getUrlManager()->createUrl($route, $params);
}
/**
* Creates an absolute URL using the given route and parameters.
* This method first calls [[createUrl()]] to create a relative URL.
* It then prepends [[\yii\web\UrlManager::hostInfo]] to the URL to form an absolute one.
* @param string $route the route. This can be either an absolute or a relative route.
* See [[createUrl()]] for more details.
* @param array $params the parameters (name-value pairs)
* @return string the created URL
* @see createUrl
*/
public function createAbsoluteUrl($route, $params = array())
{
return $this->getUrlManager()->getHostInfo() . $this->createUrl($route, $params);
}
/**
* Registers the core application components. * Registers the core application components.
* @see setComponents * @see setComponents
*/ */
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
namespace yii\web; namespace yii\web;
use Yii;
/** /**
* Controller is the base class of Web controllers. * Controller is the base class of Web controllers.
* *
...@@ -16,4 +18,27 @@ namespace yii\web; ...@@ -16,4 +18,27 @@ namespace yii\web;
*/ */
class Controller extends \yii\base\Controller class Controller extends \yii\base\Controller
{ {
/**
* Creates a URL using the given route and parameters.
*
* This method enhances [[UrlManager::createUrl()]] by supporting relative routes.
* A relative route is a route without a slash, such as "view". If the route is an empty
* string, [[route]] will be used; Otherwise, [[uniqueId]] will be prepended to a relative route.
*
* After this route conversion, the method This method calls [[UrlManager::createUrl()]]
* to create a URL.
*
* @param string $route the route. This can be either an absolute route or a relative route.
* @param array $params the parameters (name-value pairs) to be included in the generated URL
* @return string the created URL
*/
public function createUrl($route, $params = array())
{
if (strpos($route, '/') === false) {
// a relative route
$route = $route === '' ? $this->getRoute() : $this->getUniqueId() . '/' . $route;
}
return Yii::$app->getUrlManager()->createUrl($route, $params);
}
} }
\ No newline at end of file
...@@ -84,12 +84,10 @@ class HttpCache extends ActionFilter ...@@ -84,12 +84,10 @@ class HttpCache extends ActionFilter
if ($this->validateCache($lastModified, $etag)) { if ($this->validateCache($lastModified, $etag)) {
header('HTTP/1.1 304 Not Modified'); header('HTTP/1.1 304 Not Modified');
return false; return false;
} else { } elseif ($lastModified !== null) {
if ($lastModified !== null) { header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
}
return true;
} }
return true;
} }
/** /**
......
...@@ -126,65 +126,6 @@ class User extends Component ...@@ -126,65 +126,6 @@ class User extends Component
private $_keyPrefix; private $_keyPrefix;
private $_access = array(); private $_access = array();
/**
* PHP magic method.
* This method is overriden so that persistent states can be accessed like properties.
* @param string $name property name
* @return mixed property value
*/
public function __get($name)
{
if ($this->hasState($name)) {
return $this->getState($name);
} else {
return parent::__get($name);
}
}
/**
* PHP magic method.
* This method is overriden so that persistent states can be set like properties.
* @param string $name property name
* @param mixed $value property value
*/
public function __set($name, $value)
{
if ($this->hasState($name)) {
$this->setState($name, $value);
} else {
parent::__set($name, $value);
}
}
/**
* PHP magic method.
* This method is overriden so that persistent states can also be checked for null value.
* @param string $name property name
* @return boolean
*/
public function __isset($name)
{
if ($this->hasState($name)) {
return $this->getState($name) !== null;
} else {
return parent::__isset($name);
}
}
/**
* PHP magic method.
* This method is overriden so that persistent states can also be unset.
* @param string $name property name
* @throws CException if the property is read only.
*/
public function __unset($name)
{
if ($this->hasState($name)) {
$this->setState($name, null);
} else {
parent::__unset($name);
}
}
/** /**
* Initializes the application component. * Initializes the application component.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment