PHP code example of locustv2 / yii2-encode-url-rule

1. Go to this page and download the library: Download locustv2/yii2-encode-url-rule library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

locustv2 / yii2-encode-url-rule example snippets


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

// /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'
    ],
]);

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`
}

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

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

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

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