Yii2-RenderDual
Make your actions compatible for Ajax requests, (*1)
Installation
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist felixmaier1989/yii2-renderdual "*"
or add, (*4)
"felixmaier1989/yii2-renderdual": "*"
to the require section of your composer.json
file., (*5)
Usage
Once the extension is installed, simply use it in your code by extending your controller:, (*6)
...
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, (*7)
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>
)