2017 © Pedro Peláez
 

yii2-extension yii2-encode-url-rule

Makes the encoding of query parameters easier when creating urls.

image

locustv2/yii2-encode-url-rule

Makes the encoding of query parameters easier when creating urls.

  • Friday, March 31, 2017
  • by Locustv2
  • Repository
  • 1 Watchers
  • 2 Stars
  • 69 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 2 Versions
  • 6 % Grown

The README.md

Encode Url Rule for Yii2

This extension enables the creation of urls with parameters that can contain array of data. This is achieved by flatenning the array into a json string and then encoded and added as query string., (*1)

Latest Stable Version Total Downloads Latest Unstable Version License, (*2)

Installation

The preferred way to install the library is through composer., (*3)

Either run, (*4)

php composer.phar require --prefer-dist locustv2/yii2-encode-url-rule

or add, (*5)

{
    "require": {
        "locustv2/yii2-encode-url-rule": "~1.0.0"
    }
}

to your composer.json file., (*6)

Configuring EncodeUrlRule

To use EncodeUrlRule, add this to your config., (*7)

'UrlManager' => [
    'ruleConfig' => [
        'class' => 'weblement\components\EncodeUrlRule,
        'paramName' => 'enc',
        'autoEncodeParams' => [
            'page',
            'userId'
        ],
    ],
],

Using EncodeUrlRule

When you are creating urls in yii, you can pass an array or parameters to the key EncodeUrlRule::$paramName that you set. All parameters that are in EncodeUrlRule::$autoEncodeParams will also be encoded and assigned to the key in your url. For example:, (*8)

// /site/url-test/?id=123&key1=value1&enc=a2V5Mj0lMjJ2YWx1ZTIlMjImdXNlcklkPTQ1NiZwYWdlPTI%253D
echo Url::to([
    '/site/url-test',
    'id' => 123,
    'key1' => 'value1',
    'userId' => 456,
    'page' => 2,
    'enc' => [
        'key2' => 'value2'
    ],
]);

In your controller action you can get the query parameters as follows, (*9)

public function actionUrlTest($id, $userId, $key2)
{
    var_dump($id); // 123
    var_dump($userId); // 456
    var_dump($key2); // value2
    var_dump(Yii::$app->request->get('key1')); // value1
    var_dump(Yii::$app->request->get('page')); // 2
    var_dump(Yii::$app->request->get()); // contains all get query  parameters including `enc`
}

Further Examples

You are able to also pass arrays as follows:, (*10)

Url::to([
    '/site/url-test',
    'enc' => [
        'user' => [
            'id' => $user->id,
            'name' => $user->name,
        ]
    ],
]);

Url::to([
    '/site/url-test',
    'enc' => [
        'user' => User::find()->asArray()->one(),
    ],
]);

However these parameters should will not work if you are trying to assign a controller action parameter., (*11)

// will not work
public function actionUrlTest($user)
{
    var_dump($user);
}

// will work
public function actionUrlTest()
{
    var_dump(Yii::$app->request->get('user'));
}

To do

  • Add unit tests

Contributing

Feel free to send pull requests., (*12)

License

For license information check the LICENSE-file., (*13)

The Versions

31/03 2017

dev-master

9999999-dev https://github.com/locustv2/

Makes the encoding of query parameters easier when creating urls.

  Sources   Download

BSD-3-Clause

The Requires

 

by Yuv Joodhisty

url yii2 array encode rule

31/03 2017

1.0.0

1.0.0.0 https://github.com/locustv2/

Makes the encoding of query parameters easier when creating urls.

  Sources   Download

BSD-3-Clause

The Requires

 

by Yuv Joodhisty

url yii2 array encode rule