2015-12-06 78 views
0

我安裝了新的Yii先進的框架。 Nginx服務器。Yii2拒絕訪問(ForbiddenHttpException)到後端控制器

以下網址工作正常: http://yii/backend/web/index.php?r=site/index

我創建使用GII和訪問新的CRUD: http://yii/backend/web/index.php?r=user/index

它顯示如下錯誤:

An Error occurred while handling another error: 
exception 'yii\web\ForbiddenHttpException' with message 'You are not allowed to perform this action.' in /private/var/www/yii/advanced/vendor/yiisoft/yii2/filters/AccessControl.php:151 
Stack trace: 
#0 /private/var/www/yii/advanced/vendor/yiisoft/yii2/filters/AccessControl.php(134): yii\filters\AccessControl->denyAccess(Object(yii\web\User)) 
#1 /private/var/www/yii/advanced/vendor/yiisoft/yii2/base/ActionFilter.php(71): yii\filters\AccessControl->beforeAction(Object(yii\web\ErrorAction)) 
#2 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent)) 
#3 /private/var/www/yii/advanced/vendor/yiisoft/yii2/base/Component.php(541): call_user_func(Array, Object(yii\base\ActionEvent)) 
#4 /private/var/www/yii/advanced/vendor/yiisoft/yii2/base/Controller.php(263): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent)) 
#5 /private/var/www/yii/advanced/vendor/yiisoft/yii2/web/Controller.php(108): yii\base\Controller->beforeAction(Object(yii\web\ErrorAction)) 
#6 /private/var/www/yii/advanced/vendor/yiisoft/yii2/base/Controller.php(149): yii\web\Controller->beforeAction(Object(yii\web\ErrorAction)) 
#7 /private/var/www/yii/advanced/vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('error', Array) 
#8 /private/var/www/yii/advanced/vendor/yiisoft/yii2/web/ErrorHandler.php(85): yii\base\Module->runAction('site/error') 
#9 /private/var/www/yii/advanced/vendor/yiisoft/yii2/base/ErrorHandler.php(109): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException)) 
#10 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException)) 
#11 {main} 
Previous exception: 
exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "user/index".' in /private/var/www/yii/advanced/vendor/yiisoft/yii2/base/Module.php:461 
Stack trace: 
#0 /private/var/www/yii/advanced/vendor/yiisoft/yii2/web/Application.php(84): yii\base\Module->runAction('user/index', Array) 
#1 /private/var/www/yii/advanced/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request)) 
#2 /private/var/www/yii/advanced/backend/web/index.php(18): yii\base\Application->run() 
#3 {main} 

Next exception 'yii\web\NotFoundHttpException' with message 'Page not found.' in /private/var/www/yii/advanced/vendor/yiisoft/yii2/web/Application.php:96 
Stack trace: 
#0 /private/var/www/yii/advanced/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request)) 
#1 /private/var/www/yii/advanced/backend/web/index.php(18): yii\base\Application->run() 
#2 {main} 

難道我錯過了任何配置?

+0

可以用GII ...... – scaisEdge

+0

@scaisEdge相關文件是在正確的路徑 – sathish

+0

檢查生成供應商diir的permissione確保您有在路徑的一些錯誤可執行權限給大家 – scaisEdge

回答

0

Yii2 isset AccessControl的

public function behaviors() 
{ 
return [ 
    'access' => [ 
     'class' => \yii\filters\AccessControl::className(), 
     'only' => ['create', 'update'], 
     'rules' => [ 
      // deny all POST requests 
      [ 
       'allow' => false, 
       'verbs' => ['POST'] 
      ], 
      // allow authenticated users 
      [ 
       'allow' => true, 
       'roles' => ['@'], 
      ], 
      // everything else is denied 
     ], 
    ], 
]; 
} 
+0

我試過這個,但是dint工作.. – sathish

1

exception 'yii\web\ForbiddenHttpException' with message 'You are not allowed to perform this action.' in /private/var/www/yii/advanced/vendor/yiisoft/yii2/filters/AccessControl.php:151

這裏是yii2代碼

/** 
    * Denies the access of the user. 
    * The default implementation will redirect the user to the login page if he is a guest; 
    * if the user is already logged, a 403 HTTP exception will be thrown. 
    * @param User $user the current user 
    * @throws ForbiddenHttpException if the user is already logged in. 
    */ 
    protected function denyAccess($user) 
    { 
     if ($user->getIsGuest()) { 
      $user->loginRequired(); 
     } else { 
      throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); 
//this is 152 line 
     } 
    } 

所以我覺得它很清楚,你需要先登錄,所以去http://yii/backend/web/index.php?r=user/login。然後

如果不要有登錄用戶/登錄頁面,在你的UserController的頂部移除所有behaviors部分。

public function behaviors() 
    { 
. 
. 
.   
    } 
-2

這可能是由於對相同的動作或方法運行了一次以上。請避免對同一動作或方法的多個請求。這可以通過警予\基地\ ActionFilter類的overring保護功能來處理

+0

請提供一些對Questioner的參考,這樣就可以知道如何解決提供的問題。 – CodeChanger

+0

在您的common/components目錄中爲AccessControl創建一個自己的類,並將其用作'as access'=>'class'=>'common \ components \ AccessControl', – Samsher