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.
コード例 #1
0
ファイル: SortTest.php プロジェクト: sciurodont/yii2
 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'));
 }
コード例 #2
0
ファイル: Html.php プロジェクト: kalibao/magesko
 /**
  * Generates a hyperlink that can be clicked to cause sorting.
  * @param Sort $sort the current Sort instance
  * @param string $attribute the attribute name
  * @param array $options additional HTML attributes for the hyperlink tag.
  * There is one special attribute `label` which will be used as the label of the hyperlink.
  * If this is not set, the label defined in [[attributes]] will be used.
  * If no label is defined, [[\yii\helpers\Inflector::camel2words()]] will be called to get a label.
  * Note that it will not be HTML-encoded.
  * @return string
  */
 public static function sortLink(Sort $sort, $attribute, $options = [])
 {
     if (($direction = $sort->getAttributeOrder($attribute)) !== null) {
         $class = $direction === SORT_DESC ? 'current-sort desc' : 'current-sort asc';
         $icon = $direction === SORT_DESC ? 'glyphicon-sort-by-alphabet-alt' : 'glyphicon-sort-by-alphabet';
         if (isset($options['class'])) {
             $options['class'] .= ' ' . $class;
         } else {
             $options['class'] = $class;
         }
     } else {
         $icon = 'glyphicon-sort-by-alphabet';
         if (isset($options['class'])) {
             $options['class'] .= ' asc';
         } else {
             $options['class'] = 'asc';
         }
     }
     $url = $sort->createUrl($attribute);
     $options['data-sort'] = $sort->createSortParam($attribute);
     if (isset($options['label'])) {
         $label = $options['label'];
         unset($options['label']);
     } else {
         if (isset($sort->attributes[$attribute]['label'])) {
             $label = $sort->attributes[$attribute]['label'];
         } else {
             $label = Inflector::camel2words($attribute);
         }
     }
     $label .= ' <span class="glyphicon ' . $icon . '"></span>';
     return Html::a($label, $url, $options);
 }