PHPackages                             srv44/yii2-ajax-actions-behavior - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. srv44/yii2-ajax-actions-behavior

ActiveYii2-extension[Parsing &amp; Serialization](/categories/parsing)

srv44/yii2-ajax-actions-behavior
================================

Behavior auto set JSON response to actions for Yii2 Framework.

v1.0.2(5y ago)015MITPHPPHP &gt;=5.4.0

Since Sep 13Pushed 5y ago1 watchersCompare

[ Source](https://github.com/srv44/yii2-ajax-actions-behavior)[ Packagist](https://packagist.org/packages/srv44/yii2-ajax-actions-behavior)[ RSS](/packages/srv44-yii2-ajax-actions-behavior/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (2)Versions (5)Used By (0)

Ajax actions behavior for Yii2 Framework.
=========================================

[](#ajax-actions-behavior-for-yii2-framework)

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

Getting Started
---------------

[](#getting-started)

### Installing

[](#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

[](#usage)

#### AjaxActionsBehavior

[](#ajaxactionsbehavior)

Add in controller behaviors

##### Simple:

[](#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:

[](#with-disable-afteraction-validate-returned-value)

```
use srv44\AjaxActions\AjaxActionsBehavior;

...

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

...
```

#### Actions

[](#actions)

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

##### Example:

[](#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' =&gt; (bool)

```
...

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

...
```

#### AjaxActionsHelper

[](#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

[](#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
-----------------

[](#running-the-tests)

PhpUnit /tests

```
vendor/bin/phpunit
```

License
-------

[](#license)

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

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

2067d ago

Major Versions

v0.0.1 → v1.02020-09-14

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/71197970?v=4)[Aleksey](/maintainers/srv44)[@srv44](https://github.com/srv44)

---

Top Contributors

[![srv44](https://avatars.githubusercontent.com/u/71197970?v=4)](https://github.com/srv44 "srv44 (9 commits)")

---

Tags

jsonyii2ajaxBehavioryii 2

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/srv44-yii2-ajax-actions-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/srv44-yii2-ajax-actions-behavior/health.svg)](https://phpackages.com/packages/srv44-yii2-ajax-actions-behavior)
```

###  Alternatives

[kdn/yii2-json-editor

JSON editor widget (josdejong/jsoneditor) for Yii 2.

22570.0k3](/packages/kdn-yii2-json-editor)[nizsheanez/yii2-json-rpc

A lightweight JsonRpc Server and Client for PHP

2034.0k](/packages/nizsheanez-yii2-json-rpc)[devgroup/yii2-jsoneditor

Yii2 jsoneditor widget

1199.6k1](/packages/devgroup-yii2-jsoneditor)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
