2017-08-15 53 views
0

您好,我建立了一個控制器,控制器文件夾 中我tryed來訪問我的控制器和視圖的文件夾中,但我不能老是報錯404 請告訴我是什麼問題訪問在YII2子控制器和視圖

這裏是細節 這是SiteUserController在控制器/ userzone /文件夾

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 
     ]); 
    } 
} 

視圖文件是在查看/ siteuser/index.php文件目錄。
我改變URL經理

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

     ], 
    ], 
+1

yii2不支持此功能。請檢查以下網址:https://github.com/yiisoft/yii2/issues/787#issuecomment-23007144 – user773440

回答

0

在控制器:

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

     'model'=>$model 

    ]); 

其工作

0

您需要更改controllerNamespace在IE瀏覽器下面的配置代碼配置/主文件/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, 
       ];