yii\data\Sort::createUrl PHP Method

createUrl() public method

This method will consider the current sorting status given by [[attributeOrders]]. 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.
See also: attributeOrders
See also: params
public createUrl ( string $attribute, boolean $absolute = false ) : string
$attribute string the attribute name
$absolute boolean whether to create an absolute URL. Defaults to `false`.
return string the URL for sorting. False if the attribute is invalid.
    public function createUrl($attribute, $absolute = false)
    {
        if (($params = $this->params) === null) {
            $request = Yii::$app->getRequest();
            $params = $request instanceof Request ? $request->getQueryParams() : [];
        }
        $params[$this->sortParam] = $this->createSortParam($attribute);
        $params[0] = $this->route === null ? Yii::$app->controller->getRoute() : $this->route;
        $urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager;
        if ($absolute) {
            return $urlManager->createAbsoluteUrl($params);
        } else {
            return $urlManager->createUrl($params);
        }
    }

Usage Example

示例#1
0
 public function testCreateUrl()
 {
     $manager = new UrlManager(['baseUrl' => '/index.php', 'cache' => null]);
     $sort = new Sort(['attributes' => ['age', 'name' => ['asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC], 'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC]]], 'params' => ['sort' => 'age,-name'], 'enableMultiSort' => true, 'urlManager' => $manager, 'route' => 'site/index']);
     $this->assertEquals('/index.php?r=site%2Findex&sort=-age%2C-name', $sort->createUrl('age'));
     $this->assertEquals('/index.php?r=site%2Findex&sort=name%2Cage', $sort->createUrl('name'));
 }
All Usage Examples Of yii\data\Sort::createUrl