Commit cd1b3d32 by Qiang Xue

refactored url creation shortcut method.

parent 8724d803
......@@ -78,7 +78,7 @@ class Application extends Module
*/
public $preload = array();
/**
* @var Controller the currently active controller instance
* @var \yii\web\Controller|\yii\console\Controller the currently active controller instance
*/
public $controller;
/**
......@@ -355,7 +355,7 @@ class Application extends Module
/**
* Returns the request component.
* @return Request the request component
* @return \yii\web\Request|\yii\console\Request the request component
*/
public function getRequest()
{
......
......@@ -949,11 +949,10 @@ class Html
* If the input parameter
*
* - 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,
* will be resolved into a URL;
* - is a non-empty string: it will be processed by [[Yii::getAlias()]] and returned;
* - 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()]].
* Here are some examples: `array('post/index', 'page' => 2)`, `array('index')`.
* pairs are treated as the parameters to be used for URL creation using [[\yii\web\Controller::createUrl()]].
* For example: `array('post/index', 'page' => 2)`, `array('index')`.
*
* @param array|string $url the parameter to be used to generate a valid URL
* @return string the normalized URL
......@@ -963,7 +962,13 @@ class Html
{
if (is_array($url)) {
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 {
throw new InvalidParamException('The array specifying a URL must contain at least one element.');
}
......
......@@ -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.
* @see setComponents
*/
......
......@@ -7,6 +7,8 @@
namespace yii\web;
use Yii;
/**
* Controller is the base class of Web controllers.
*
......@@ -16,4 +18,27 @@ namespace yii\web;
*/
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,13 +84,11 @@ class HttpCache extends ActionFilter
if ($this->validateCache($lastModified, $etag)) {
header('HTTP/1.1 304 Not Modified');
return false;
} else {
if ($lastModified !== null) {
} elseif ($lastModified !== null) {
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
}
return true;
}
}
/**
* Validates if the HTTP cache contains valid content.
......
......@@ -126,65 +126,6 @@ class User extends Component
private $_keyPrefix;
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.
......
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