访问YII2中的子文件夹控制器和视图

时间:2017-08-15 14:00:54

标签: yii yii2 yii2-basic-app

您好我在控制器文件夹内的文件夹中构建了一个控制器 我试图访问我的控制器及其视图,但我不能总是错误404 请告诉我这是什么问题

这里是细节 这是Controllers / userzone /文件夹中的SiteUserController

namespace app\controllers\userzone;


use yii\web\Controller;
use app\models\UserZone;
/**
 * Default controller for the `dashboard` module
 */
class SiteUserController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        $id = \Yii::$app->user->id;

        $model = UserZone::find()->where(['id_zone'=>$id])->with('user')->one();

        //   $model->joinWith('companiesCompany');


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

视图文件位于Views / siteuser / index.php目录中。
我将网址管理员更改为

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            'userzone/<controller:\w+>/<action:\w+>'=>'userzone/<controller>/<action>',

        ],
    ],

2 个答案:

答案 0 :(得分:0)

在控制器中:

return $this->render('index',[

        'model'=>$model

    ]);

其工作

答案 1 :(得分:0)

您需要在config / main文件中更改controllerNamespace,即在config / main.php的代码下面

                <?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' => 'app\controllers\userzone', //here is your controller path
                'components' => [
                    'view' => [
                        'theme' => [

                            'pathMap' => [
                                '@frontend/views' => '@themes/frontend/views',
                            ],
                        ],
                    ],
                    'request' => [
                        'csrfParam' => '_csrf-frontend',
                    ],
                    'user' => [
                        'identityClass' => 'common\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' => [
                        ],
                    ],
                    */
                ],
                'params' => $params,
                ];