Controller::renderAjax()
method can be used to respond to an Ajax request. This method is similar to renderPartial() except that it will inject into the rendering result with JS/CSS scripts and files which are registered with the view
Assume we have login form in a view file:
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
\yii\bootstrap\BootstrapAsset::register($this);
<div class="site-login">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'username')->textInput() ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= Html::submitButton('Login',['class' => 'btn btn-primary btn-block']) ?>
<?php ActiveForm::end(); ?>
</div>
When we use renderPartial()
in a controller action:
public function actionLogin()
{
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
return $this->renderPartial('login', [
'model' => $model,
]);
}
Action output:
<div class="site-login">
<form id="w0" action="/site/login" method="post" role="form">
<div class="form-group field-loginform-username required">
<label class="control-label" for="loginform-username">Имя пользователя</label>
<input type="text" id="loginform-username" class="form-control" name="LoginForm[username]">
</div>
<div class="form-group field-loginform-password required">
<label class="control-label" for="loginform-password">Пароль</label>
<input type="password" id="loginform-password" class="form-control" name="LoginForm[password]">
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
</form>
</div>
When we use renderAjax()
in a controller action:
...
return $this->renderAjax('login', [
'model' => $model,
]);
...
Action output (JS,CSS injected):
<link href="/assets/f1759119/css/bootstrap.css" rel="stylesheet">
<div class="site-login">
<form id="w0" action="/site/login" method="post" role="form">
<div class="form-group field-loginform-username required">
<label class="control-label" for="loginform-username">Имя пользователя</label>
<input type="text" id="loginform-username" class="form-control" name="LoginForm[username]">
</div>
<div class="form-group field-loginform-password required">
<label class="control-label" for="loginform-password">Пароль</label>
<input type="password" id="loginform-password" class="form-control" name="LoginForm[password]">
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
</form>
</div>
<script src="/assets/13aa7b5d/jquery.js"></script>
<script src="/assets/302a2946/yii.js"></script>
<script src="/assets/302a2946/yii.validation.js"></script>
<script src="/assets/302a2946/yii.activeForm.js"></script>
If we want to exclude some assets from view (to prevent dublicates):
...
Yii::$app->assetManager->bundles = [
'yii\bootstrap\BootstrapAsset' => false,
];
return $this->renderAjax('login', [
'model' => $model,
]);
...
Action output (no bootstrap.css):
<div class="site-login">
<form id="w0" action="/site/login" method="post" role="form">
<div class="form-group field-loginform-username required">
<label class="control-label" for="loginform-username">Имя пользователя</label>
<input type="text" id="loginform-username" class="form-control" name="LoginForm[username]">
</div>
<div class="form-group field-loginform-password required">
<label class="control-label" for="loginform-password">Пароль</label>
<input type="password" id="loginform-password" class="form-control" name="LoginForm[password]">
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
</form>
</div>
<script src="/assets/13aa7b5d/jquery.js"></script>
<script src="/assets/302a2946/yii.js"></script>
<script src="/assets/302a2946/yii.validation.js"></script>
<script src="/assets/302a2946/yii.activeForm.js"></script>