srv44 / yii2-ajax-actions-behavior
Behavior auto set JSON response to actions for Yii2 Framework.
Installs: 15
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=5.4.0
- yiisoft/yii2: ~2.0
Requires (Dev)
- phpunit/phpunit: 6.*
This package is auto-updated.
Last update: 2025-06-14 18:44:36 UTC
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