承接 srv44/yii2-ajax-actions-behavior 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

srv44/yii2-ajax-actions-behavior

Composer 安装命令:

composer require srv44/yii2-ajax-actions-behavior

包简介

Behavior auto set JSON response to actions for Yii2 Framework.

README 文档

README

Simple yii2 behavior, what auto set JSON response format to actions.

Getting Started

Installing

Run in your terminal

composer require srv44/yii2-ajax-actions-behavior ^1.0

Or add

"srv44/yii2-ajax-actions-behavior" : "^1.0"

to the require section of your application's composer.json file.

Usage

AjaxActionsBehavior

Add in controller behaviors

Simple:

Note! By default, AjaxActionsBehavior::checkResponse = true. It`s run afterAction validation of returned value.

use srv44\AjaxActions\AjaxActionsBehavior;

...

public function behaviors()
{
    return [
        ...
        AjaxActionsBehavior::className(),
    ];
}
With disable afterAction validate returned value:
use srv44\AjaxActions\AjaxActionsBehavior;

...

public function behaviors()
{
    return [
        ...
        [
            'class' => AjaxActionsBehavior::className(),
            'checkResponse' => false
        ],
    ];
}

...

Actions

You ajax actions name must be start with actionAjax{YourAction}.

Example:
...

public function actionAjaxYourAction() 
{
    ...
}

...

If you disabled AjaxActionsBehavior::checkResponse, you may return any value.

...

public function actionAjaxYourAction() 
{
    return 'some value';
}
// result: 'some value'

...

By default, you need return array value with 'success' => (bool)

...

public function actionAjaxYourAction() 
{
    return ['success' => true, 'your' => 'value'];
}
// result: {"success":true,"your":"value"}

...

AjaxActionsHelper

To make things easier, you can use the AjaxActionsHelper.

It is a simple helper that provides several static methods.

The most necessary are success([array $params]) and error([string $errorMsg, array $params, string $errorMsgKey])

Examples

AjaxActionsHelper::success

use srv44\AjaxActions\AjaxActionsHelper;

...

// without params
public function actionAjaxWithoutParams() 
{
    return AjaxActionsHelper::success();
}
// result: {"success":true}

// with params
public function actionAjaxWithParams() 
{
    return AjaxActionsHelper::success(['your' => 'value']);
}
// result: {"success":true}

...

AjaxActionsHelper::error

use srv44\AjaxActions\AjaxActionsHelper;

...

// Without params
public function actionAjaxWithoutParams() 
{
    return AjaxActionsHelper::error();
}
// result: {"success":false, "errorMessage":"Error!"}

// Only error message
public function actionAjaxOnlyErrorMessage() 
{
    return AjaxActionsHelper::error('Bad request!');
}
// result: {"success":false, "errorMessage":"Bad request!"}

// With error message and params
public function actionAjaxWithErrorMessageAndParams() 
{
    return AjaxActionsHelper::error('Bad request!', ['your' => 'value']);
}
// result: {"success":false, "errorMessage":"Bad request!", "your" => "value"}

// With custom error message field
public function actionAjaxWithCustomErrorField() 
{
    return AjaxActionsHelper::error('Bad request!', ['your' => 'value'], 'myErrorMessage');
}
// result: {"success":false, "myErrorMessage":"Bad request!", "your" => "value"}

...

Running the tests

PhpUnit /tests

vendor/bin/phpunit

License

This project is licensed under the MIT License - see the LICENSE.md file for details

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固