Browse Source

Paginiation and Sort can now create absolute URLs

tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
37e22e577e
  1. 1
      framework/CHANGELOG.md
  2. 15
      framework/yii/data/Pagination.php
  3. 7
      framework/yii/data/Sort.php

1
framework/CHANGELOG.md

@ -21,6 +21,7 @@ Yii Framework 2 Change Log
- Enh: Added `favicon.ico` and `robots.txt` to defauly application templates (samdark) - Enh: Added `favicon.ico` and `robots.txt` to defauly application templates (samdark)
- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue) - Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)
- Enh: Support for file aliases in console command 'message' (omnilight) - Enh: Support for file aliases in console command 'message' (omnilight)
- Enh: Sort and Paginiation can now create absolute URLs (cebe)
- Chg: Renamed yii\jui\Widget::clientEventsMap to clientEventMap (qiangxue) - Chg: Renamed yii\jui\Widget::clientEventsMap to clientEventMap (qiangxue)
- New #1438: [MongoDB integration](https://github.com/yiisoft/yii2-mongodb) ActiveRecord and Query (klimov-paul) - New #1438: [MongoDB integration](https://github.com/yiisoft/yii2-mongodb) ActiveRecord and Query (klimov-paul)
- New #1393: [Codeception testing framework integration](https://github.com/yiisoft/yii2-codeception) (Ragazzo) - New #1393: [Codeception testing framework integration](https://github.com/yiisoft/yii2-codeception) (Ragazzo)

15
framework/yii/data/Pagination.php

@ -91,6 +91,11 @@ class Pagination extends Object
*/ */
public $params; public $params;
/** /**
* @var \yii\web\UrlManager the URL manager used for creating pagination URLs. If not set,
* the "urlManager" application component will be used.
*/
public $urlManager;
/**
* @var boolean whether to check if [[page]] is within valid range. * @var boolean whether to check if [[page]] is within valid range.
* When this property is true, the value of [[page]] will always be between 0 and ([[pageCount]]-1). * When this property is true, the value of [[page]] will always be between 0 and ([[pageCount]]-1).
* Because [[pageCount]] relies on the correct value of [[totalCount]] which may not be available * Because [[pageCount]] relies on the correct value of [[totalCount]] which may not be available
@ -167,11 +172,12 @@ class Pagination extends Object
* Creates the URL suitable for pagination with the specified page number. * Creates the URL suitable for pagination with the specified page number.
* This method is mainly called by pagers when creating URLs used to perform pagination. * This method is mainly called by pagers when creating URLs used to perform pagination.
* @param integer $page the zero-based page number that the URL should point to. * @param integer $page the zero-based page number that the URL should point to.
* @param boolean $absolute whether to create an absolute URL. Defaults to `false`.
* @return string the created URL * @return string the created URL
* @see params * @see params
* @see forcePageVar * @see forcePageVar
*/ */
public function createUrl($page) public function createUrl($page, $absolute = false)
{ {
if (($params = $this->params) === null) { if (($params = $this->params) === null) {
$request = Yii::$app->getRequest(); $request = Yii::$app->getRequest();
@ -183,7 +189,12 @@ class Pagination extends Object
unset($params[$this->pageVar]); unset($params[$this->pageVar]);
} }
$route = $this->route === null ? Yii::$app->controller->getRoute() : $this->route; $route = $this->route === null ? Yii::$app->controller->getRoute() : $this->route;
return Yii::$app->getUrlManager()->createUrl($route, $params); $urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager;
if ($absolute) {
return $urlManager->createAbsoluteUrl($route, $params);
} else {
return $urlManager->createUrl($route, $params);
}
} }
/** /**

7
framework/yii/data/Sort.php

@ -329,12 +329,13 @@ class Sort extends Object
* For example, if the current page already sorts the data by the specified attribute in ascending order, * For example, if the current page already sorts the data by the specified attribute in ascending order,
* then the URL created will lead to a page that sorts the data by the specified attribute in descending order. * then the URL created will lead to a page that sorts the data by the specified attribute in descending order.
* @param string $attribute the attribute name * @param string $attribute the attribute name
* @param boolean $absolute whether to create an absolute URL. Defaults to `false`.
* @return string the URL for sorting. False if the attribute is invalid. * @return string the URL for sorting. False if the attribute is invalid.
* @throws InvalidConfigException if the attribute is unknown * @throws InvalidConfigException if the attribute is unknown
* @see attributeOrders * @see attributeOrders
* @see params * @see params
*/ */
public function createUrl($attribute) public function createUrl($attribute, $absolute = false)
{ {
if (($params = $this->params) === null) { if (($params = $this->params) === null) {
$request = Yii::$app->getRequest(); $request = Yii::$app->getRequest();
@ -343,8 +344,12 @@ class Sort extends Object
$params[$this->sortVar] = $this->createSortVar($attribute); $params[$this->sortVar] = $this->createSortVar($attribute);
$route = $this->route === null ? Yii::$app->controller->getRoute() : $this->route; $route = $this->route === null ? Yii::$app->controller->getRoute() : $this->route;
$urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager; $urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager;
if ($absolute) {
return $urlManager->createAbsoluteUrl($route, $params);
} else {
return $urlManager->createUrl($route, $params); return $urlManager->createUrl($route, $params);
} }
}
/** /**
* Creates the sort variable for the specified attribute. * Creates the sort variable for the specified attribute.

Loading…
Cancel
Save