phpyii2yii2-advanced-app

Yii2 controller extends another controller


I want to extend controller from another controller

I have controller "ProjectController" and "MembersController"; is it possible to extend Members controller from Project controller.

so I can get path like:

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

ProjectController

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

And Members Controller

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

I use class MemebrsController extends ProjectController but I get a 404 error page when I access this url

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

my config file is

<?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,
];

Solution

  • In OOP it's not good idea to extend MemberController form ProjectController.

    Crate MemberController and extend it from Controller and then add below rule to your route:

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