【问题标题】:Yii2 Executes the view twiceYii2 执行两次视图
【发布时间】:2015-12-27 15:08:52
【问题描述】:

我已经更改了登录操作的索引操作,现在视图像这样执行两次

为什么会这样?

谁能帮帮我?

这是布局:

    <?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();
    }

}

这是第一次发生在我身上。

【问题讨论】:

  • 上下文和代码是我们所需要的,清楚地证明之前的努力(部分是为了让我们可以看到您已经尝试过什么,所以最好是带有描述的代码)这也是需要的。 stackoverflow.com/help/how-to-ask
  • 也显示控制器。
  • 能不能把上面的视图文件名和路径也加进去?我不确定您作为视图提到的文件是“登录”,或者您的“布局”是否命名为登录。
  • 视图名称为index,布局名称为login
  • 奇怪。会不会是你混淆了任何文件?让我感到奇怪的是,带有 bg-pic 类的 div 不在屏幕截图中。如果 $content 会被渲染两次,那么它也应该可见两次。

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


【解决方案1】:

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

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

另一种可能是在您的控制器代码中多次调用echo $this-&gt;render('yourview',...)。您应该始终返回而不是回显那些渲染调用,例如return $this-&gt;render('yourview',...) 代替。然后就是不能多次渲染。

【讨论】:

  • 我也认为是布局,但我看不到与平常不同的东西
  • 我发现了问题,它是来自 apache 的 mod_rewrite mod。工作不正常,结果就是这样。
猜你喜欢
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 2017-04-27
  • 2011-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多