Yii2 控制器扩展另一个控制器

Yii2 controller extends another controller

我想从另一个控制器扩展控制器

我有控制器 "ProjectController" 和 "MembersController";是否可以从项目控制器扩展成员控制器。

所以我可以获得如下路径:

http://localhost:125/yii2/frontend/web/project/members/view?m=12345

项目控制器

<?php

namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

use frontend\models\Project;

/***/
class ProjectController extends Controller
{
    public function actionView($p)
    {
        $model = $this->findProject($id);

        return $this->render('/project/view', [
                'model' => $model,
            ]);
    }

    protected function findProject($id)
    {
        if (($model = Project::findOne(['project_id' => $id])) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}

和会员控制器

<?php

namespace frontend\controllers;

use Yii;
use frontend\controllers\ProjectController;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

use frontend\models\Members;

/***/
class MembersController extends ProjectController
{
    public function actionView($m)
    {
        $model = $this->findMembers($m);

        return $this->render('/members/view', [
                'model' => $model,
            ]);
    }

    protected function findMembers($id)
    {
        if (($model = Members::find()->where(['sproject_id' => $id])->all()) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}

我使用 class MemebrsController extends ProjectController,但是当我访问此 url

时出现 404 错误页面
http://localhost:125/yii2/frontend/web/project/members/view?m=123

我的配置文件是

<?php
$params = array_merge(
    require __DIR__ . '/../../common/config/params.php',
    require __DIR__ . '/../../common/config/params-local.php',
    require __DIR__ . '/params.php',
    require __DIR__ . '/params-local.php'
);

return [
'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',

    'components' => [
        'request' => [
            'csrfParam' => '_csrf-frontend',
        ],
        'user' => [
            'identityClass' => 'frontend\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the frontend
            'name' => 'advanced-frontend',
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        /* ****************** */
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '<action:(index|login|logout|signup)>' => 'site/<action>',
            ],
        ],
    ],
    'params' => $params,
];

如果您使用成员扩展了项目控制器,那么您应该使用

http://localhost:125/yii2/frontend/web/members/view?m=123

并检查名称是否正确。你有 MemebrsController 而不是 MembersController

在 OOP 中扩展 MemberController 形式 ProjectController 不是个好主意。

创建 MemberController 并从 Controller 扩展它,然后将以下规则添加到您的路线:

'rules' => [
    'project/member' => 'member',
     // ... Other rules
]