Yii2执行两次视图

时间:2015-09-30 00:18:11

标签: php model-view-controller layout view yii2

我已经更改了登录操作的索引操作,现在视图执行了两次,如enter image description here

为什么会这样?

任何人都可以帮助我吗?

这是布局:

    <?php
/* @var $this \yii\web\View */
/* @var $content string */

use yii\helpers\Html;
use app\assets\LoginAsset;

LoginAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
    <head>
        <meta charset="<?= Yii::$app->charset ?>">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <?= Html::csrfMetaTags() ?>
        <title><?= Html::encode($this->title) ?></title>
        <?php $this->head() ?>
    </head>    
    <body class="fixed-header">
        <?php $this->beginBody() ?>

        <div class="login-wrapper">
            <?= $content ?>
        </div>

        <?php $this->endBody() ?>
    </body>
</html>
<?php $this->endPage() ?>

这是观点:

<?php
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model app\models\LoginForm */

use yii\bootstrap\ActiveForm;
use yii\helpers\Url;
use yii\helpers\Html;

$this->title = 'Tramite al dia - Login';
?>

<div class="bg-pic">
    <!-- START Background Pic 
    <img src="<?= Url::base() ?>/public/pages/assets/img/demo/new-york-city-buildings-sunrise-morning-hd-wallpaper.jpg" height="1200" width="1920" class="lazy">  -->    
    <!-- END Background Pic -->
    <!-- START Background Caption-->
    <div class="bg-caption pull-bottom sm-pull-bottom text-white p-l-20 m-b-20">
        <h2 class="semi-bold text-white">
            En la facilidad esta la felicidad de las personas.
        </h2>
        <p class="small">
            Te facilitamos el control del día a día con el tramite y gestión de sus documentos, <br> Tramite al día &copy; 2015
        </p>
    </div>
    <!-- END Background Caption-->
</div>

<div class="login-container bg-white">
    <div class="p-l-50 m-l-20 p-r-50 m-r-20 p-t-50 m-t-30 sm-p-l-15 sm-p-r-15 sm-p-t-40">
        <!-- <img src="<?= Url::base() ?>/public/pages/assets/img/logo.png" alt="logo" width="78" height="22"> -->    
        <p class="p-t-35">Accede a tu cuenta</p>
        <!-- START Login Form -->
        <?php $form = ActiveForm::begin(); ?>        

        <!-- START Form Control-->
        <div class="form-group form-group-default">
            <label>Usuario</label>
            <div class="controls">
                <?= $form->field($model, "usuario")->label(FALSE); ?>                
            </div>
        </div>
        <!-- END Form Control-->
        <!-- START Form Control-->
        <div class="form-group form-group-default">
            <label>Contraseña</label>
            <div class="controls">
                <?= $form->field($model, "clave")->passwordInput()->label(FALSE); ?>
            </div>
        </div>
        <!-- START Form Control-->
        <div class="row">
            <div class="col-md-6 no-padding">                
                <?= $form->field($model, "recuerdaMe")->checkbox()->label(FALSE) ?>
                <label>Mantener sesión iniciada</label>                
            </div>
            <div class="col-md-6 text-right m-t-10">
                <a href="#" class="text-info small">Necesita ayuda?</a>
            </div>
        </div>
        <!-- END Form Control-->
        <?= Html::submitButton("Entrar", ["class" => "btn btn-primary btn-cons m-t-10"]) ?>        

        <?php ActiveForm::end(); ?>
        <!--END Login Form-->
    </div>
</div>

这是控制器

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;

class SiteController extends Controller {

    public $layout = "login";

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex() {
        $model = new LoginForm();

        if ($model->load(Yii::$app->request->post()) && $model->login()):
            return $this->redirect(["landing/index"]);
        else:
            return $this->render('index', [
                        'model' => $model,
            ]);
        endif;
    }    

    public function actionLogout() {
        Yii::$app->user->logout();

        return $this->goHome();
    }

}

第一次发生在我身上。

1 个答案:

答案 0 :(得分:0)

我很确定它是关于布局文件中的渲染。显示views/layouts/main.php的代码,如果您还有子模板。例如。如果您有以下代码的多个部分,您将获得您提到的结果,例如$content的多重回声。

<?php $this->beginBody() ?>
<?= $content ?>
<?php $this->endBody() ?>

另一种可能是在您的控制器代码中多次调用echo $this->render('yourview',...)。你应该总是返回并且不回应那些渲染调用,例如而是return $this->render('yourview',...)。然后无法多次渲染。