Browse Source

refactored url creation shortcut method.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
cd1b3d321a
  1. 4
      framework/base/Application.php
  2. 15
      framework/helpers/Html.php
  3. 45
      framework/web/Application.php
  4. 25
      framework/web/Controller.php
  5. 8
      framework/web/HttpCache.php
  6. 59
      framework/web/User.php

4
framework/base/Application.php

@ -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()
{

15
framework/helpers/Html.php

@ -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.');
}

45
framework/web/Application.php

@ -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
*/

25
framework/web/Controller.php

@ -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);
}
}

8
framework/web/HttpCache.php

@ -84,12 +84,10 @@ class HttpCache extends ActionFilter
if ($this->validateCache($lastModified, $etag)) {
header('HTTP/1.1 304 Not Modified');
return false;
} else {
if ($lastModified !== null) {
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
}
return true;
} elseif ($lastModified !== null) {
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
}
return true;
}
/**

59
framework/web/User.php

@ -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.

Loading…
Cancel
Save