felixmaier1989/yii2-renderdual

Make your actions compatible for Ajax requests

1.0 2015-07-26 05:51 UTC

This package is auto-updated.

Last update: 2024-12-04 20:44:35 UTC


README

Make your actions compatible for Ajax requests

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist felixmaier1989/yii2-renderdual "*"

or add

"felixmaier1989/yii2-renderdual": "*"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by extending your controller:

...
use yii2renderdual\RenderDual;

class SiteController extends Controller
{
    public function behaviors()
    {
        return [
            ...
            \yii2renderdual\RenderDual::className()
        ];
    }

    public function actionAbout()
    {
        Yii::$app->session->setFlash('success', 'Welcome on my home page');
        $fruits = ['banana', 'apple', 'jackfruit', 'papaya'];
        return $this->renderDual('about', compact('fruits'), true);
    }
...

An Ajax call to your site/about action would then return

Array
(
    [flashes] => Array
        (
            [success] => Welcome on my home page
        )
    [params] => Array
        (
            [fruits] => Array
                (
                    [0] => banana
                    [1] => apple
                    [2] => jackfruit
                    [3] => papaya
                )

        )
    [rendered] => <h1>About</h1><p>I like banana, apple, jackfruit, papaya</p>
)