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

问题描述 投票:0回答:2

嗨我在controllers文件夹内的文件夹中构建了一个控制器 我尝试访问我的控制器及其视图,但我不能总是出现错误 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>',

        ],
    ],
php yii yii2 yii2-basic-app
2个回答
0
投票

在控制器中:

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

        'model'=>$model

    ]);

它的工作


0
投票

您需要更改 config/main 文件中的控制器命名空间,即下面的 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,
                ];
© www.soinside.com 2019 - 2024. All rights reserved.