定制 locustv2/yii2-encode-url-rule 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

locustv2/yii2-encode-url-rule

Composer 安装命令:

composer require locustv2/yii2-encode-url-rule

包简介

Makes the encoding of query parameters easier when creating urls.

README 文档

README

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.

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

The preferred way to install the library is through composer.

Either run

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

or add

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

to your composer.json file.

Configuring EncodeUrlRule

To use EncodeUrlRule, add this to your config.

'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:

// /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

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:

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.

// 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.

License

For license information check the LICENSE-file.

统计信息

  • 总下载量: 475
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-03-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固