Finished Html helper.

This commit is contained in:
Qiang Xue
2013-03-10 22:00:26 -04:00
parent d2fcc69b34
commit 30d70be071
8 changed files with 712 additions and 854 deletions

View File

@@ -6,6 +6,7 @@
*/
namespace yii\web;
use yii\base\InvalidParamException;
/**
* Application is the base class for all application classes.
@@ -62,11 +63,48 @@ class Application extends \yii\base\Application
}
/**
* @return UrlManager
* 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 slash. If the route is an empty string, it stands for
* the route of the currently active [[controller]]. If the route is not empty, it stands for
* an action ID of the [[controller]].
*
* 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 getUrlManager()
public function createUrl($route, $params = array())
{
return $this->getComponent('urlManager');
if (strpos($route, '/') === false) {
// a relative route
if ($this->controller !== null) {
$route = $route === '' ? $this->controller->route : $this->controller->uniqueId . '/' . $route;
} else {
throw new InvalidParamException('No active controller exists for resolving a relative route.');
}
}
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);
}
/**
@@ -86,9 +124,6 @@ class Application extends \yii\base\Application
'session' => array(
'class' => 'yii\web\Session',
),
'urlManager' => array(
'class' => 'yii\web\UrlManager',
),
));
}
}

View File

@@ -16,4 +16,8 @@ namespace yii\web;
*/
class Controller extends \yii\base\Controller
{
public function createUrl($route, $params = array())
{
}
}

View File

@@ -368,7 +368,7 @@ class Request extends \yii\base\Request
*/
protected function resolvePathInfo()
{
$pathInfo = $this->getRequestUri();
$pathInfo = $this->getUrl();
if (($pos = strpos($pathInfo, '?')) !== false) {
$pathInfo = substr($pathInfo, 0, $pos);
@@ -407,42 +407,41 @@ class Request extends \yii\base\Request
}
/**
* Returns the currently requested URL.
* This is a shortcut to the concatenation of [[hostInfo]] and [[requestUri]].
* @return string the currently requested URL.
* Returns the currently requested absolute URL.
* This is a shortcut to the concatenation of [[hostInfo]] and [[url]].
* @return string the currently requested absolute URL.
*/
public function getAbsoluteUrl()
{
return $this->getHostInfo() . $this->getUrl();
}
private $_url;
/**
* Returns the currently requested relative URL.
* This refers to the portion of the URL that is after the [[hostInfo]] part.
* It includes the [[queryString]] part if any.
* @return string the currently requested relative URL. Note that the URI returned is URL-encoded.
* @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration
*/
public function getUrl()
{
return $this->getHostInfo() . $this->getRequestUri();
}
private $_requestUri;
/**
* Returns the portion after [[hostInfo]] for the currently requested URL.
* This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any.
* The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework.
* @return string the request URI portion for the currently requested URL.
* Note that the URI returned is URL-encoded.
* @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration
*/
public function getRequestUri()
{
if ($this->_requestUri === null) {
$this->_requestUri = $this->resolveRequestUri();
if ($this->_url === null) {
$this->_url = $this->resolveRequestUri();
}
return $this->_requestUri;
return $this->_url;
}
/**
* Sets the currently requested URI.
* Sets the currently requested relative URL.
* The URI must refer to the portion that is after [[hostInfo]].
* Note that the URI should be URL-encoded.
* @param string $value the request URI to be set
*/
public function setRequestUri($value)
public function setUrl($value)
{
$this->_requestUri = $value;
$this->_url = $value;
}
/**