渲染父项 Class 在子项中查看 Class
Render Parent Class View in Child Class
MY LoginController class 是从 UserController 扩展而来的。我想覆盖 UserController 的一种方法。一切正常,但 $this->render('index') 调用子视图 class。我想调用父视图class。我试过使用 parent::render('index') 但也不起作用。这是我的代码
<?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,
]);
}
}
}
我得到的错误是
但我的意见是它应该调用父 class 视图而不是在子 class 中查看它。我在这里错过了什么?
视图通常不是按 class 组织的..所以你应该调用你需要的视图..
例如登录视图
return $this::render('login', [
'model' => $model,
]);
或另一个视图名称:my_new_view_login in (views/your_controller/my_new_view_login.php
)
return $this::render('my_new_view_login', [
'model' => $model,
]);
您需要使用完整路径才能查看文件。如果它是一个扩展,使用来自别名的路径
return $this->render('@mdm/admin/views/user/login', ['model' => $model]);
来自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
.
所以使用:
return $this->render('@mdm/admin/views/user/login', ['model' => $model]);
MY LoginController class 是从 UserController 扩展而来的。我想覆盖 UserController 的一种方法。一切正常,但 $this->render('index') 调用子视图 class。我想调用父视图class。我试过使用 parent::render('index') 但也不起作用。这是我的代码
<?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,
]);
}
}
}
我得到的错误是
但我的意见是它应该调用父 class 视图而不是在子 class 中查看它。我在这里错过了什么?
视图通常不是按 class 组织的..所以你应该调用你需要的视图..
例如登录视图
return $this::render('login', [
'model' => $model,
]);
或另一个视图名称:my_new_view_login in (views/your_controller/my_new_view_login.php
)
return $this::render('my_new_view_login', [
'model' => $model,
]);
您需要使用完整路径才能查看文件。如果它是一个扩展,使用来自别名的路径
return $this->render('@mdm/admin/views/user/login', ['model' => $model]);
来自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 isuser
. If there is no active module, the view file path would be@app/views/user/create.php
.
所以使用:
return $this->render('@mdm/admin/views/user/login', ['model' => $model]);