doudao2407 2017-04-11 22:39
浏览 87
已采纳

在子类中渲染父类视图

MY LoginController class is extended from UserController. I want to ovverride one method of UserController. Everything works fine but $this->render('index') call the view of child class. I want to call the view of parent class. I have tried using parent::render('index') but taht dodn't work too. Here is my code

<?php
namespace frontend\controllers;
use Yii;
use mdm\admin\models\form\Login;
use mdm\admin\models\form\PasswordResetRequest;
use mdm\admin\models\form\ResetPassword;
use mdm\admin\models\form\Signup;
use mdm\admin\models\form\ChangePassword;
use mdm\admin\models\User;
use mdm\admin\models\searchs\User as UserSearch;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\NotFoundHttpException;
use yii\base\UserException;
use yii\mail\BaseMailer;
use mdm\admin\controllers\UserController;

class LoginController extends UserController
{

   public function actionLogin()
    {
        //parent::actionLogin();
        if (!Yii::$app->getUser()->isGuest) {
            return $this->goHome();
        }

        $model = new Login();
        if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return parent::render('login', [
                    'model' => $model,
            ]);
        }
    }
}

The error i am getting is enter image description here

But my opinion is that it should call the parent class view instead of looking it in child class. What am i missing here?

展开全部

  • 写回答

3条回答 默认 最新

  • doulan2827 2017-04-11 22:51
    关注

    From the Yii 2 Guide:

    If the view name starts with a single slash /, the view file path is formed by prefixing the view name with the view path of the currently active module. If there is no active module, @app/views/ViewName will be used. For example, /user/create will be resolved into @app/modules/user/views/user/create.php, if the currently active module is user. If there is no active module, the view file path would be @app/views/user/create.php.

    So use:

    return $this->render('@mdm/admin/views/user/login', ['model' => $model]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部