Search code examples
phpyii2production

100% locally-working Yii2 site throws errors on production server


My Yii2 site worked perfectly on my local machine, however since I've uploaded it to the production server I'm getting some odd issues.

The problem is that, while I can add a record using the system, I can't delete or even view the record once it's created. The error is 403 "You are not allowed to perform this action."

I was using the below 'access' rules in my SiteController but I still have the issue even when this section is commented out.

'access' => [
   'class' => AccessControl::className(),
   'only' => ['logout', 'signup', 'create', 'edit', 'index'],
   'rules' => [
           [
           'actions' => ['show'],
           'allow' => true,
                   'roles' => ['?'],
                ],
                [
                        'actions' => ['index', 'logout', 'create', 'edit'],
                        'allow' => true,
                        'roles' => ['@'],
                ],
        ],
],

Edit: two comments around Windows/Linux case sensitivity. The first issue (which I've now deleted from my question) was caused by exactly this.

However the second issue around 403 forbidden issues is still persisting.

I can see the main "index" of each view fine, but trying to "view", "update" or "delete" is not working. As I said, "add" works, but then when it takes me to the "view" action after adding, I get the 403.

The full text of the error, as I'm now ALSO getting the same problem on my local server, is as follows:

exception 'yii\web\ForbiddenHttpException' with message 'You are not allowed to perform this action.' in D:\Websites\cabana\advanced\vendor\yiisoft\yii2\filters\AccessControl.php:154 
Stack trace: 
#0 D:\Websites\cabana\advanced\vendor\yiisoft\yii2\filters\AccessControl.php(137): yii\filters\AccessControl->denyAccess(Object(yii\web\User)) 
#1 D:\Websites\cabana\advanced\vendor\yiisoft\yii2\base\ActionFilter.php(75): yii\filters\AccessControl->beforeAction(Object(yii\base\InlineAction)) 
#2 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent)) 
#3 D:\Websites\cabana\advanced\vendor\yiisoft\yii2\base\Component.php(545): call_user_func(Array, Object(yii\base\ActionEvent)) 
#4 D:\Websites\cabana\advanced\vendor\yiisoft\yii2\base\Controller.php(272): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent)) 
#5 D:\Websites\cabana\advanced\vendor\yiisoft\yii2\web\Controller.php(164): yii\base\Controller->beforeAction(Object(yii\base\InlineAction)) 
#6 D:\Websites\cabana\advanced\vendor\yiisoft\yii2\base\Controller.php(154): yii\web\Controller->beforeAction(Object(yii\base\InlineAction)) 
#7 D:\Websites\cabana\advanced\vendor\yiisoft\yii2\base\Module.php(523): yii\base\Controller->runAction('view', Array) 
#8 D:\Websites\cabana\advanced\vendor\yiisoft\yii2\web\Application.php(102): yii\base\Module->runAction('job/view', Array) 
#9 D:\Websites\cabana\advanced\vendor\yiisoft\yii2\base\Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request)) 
#10 D:\Websites\cabana\advanced\backend\web\index.php(18): yii\base\Application->run() 
#11 {main}

Is there anything I should have done to put my site online? Like switch to "production" mode or something? Because I took the files from my local machine, uploaded them as-is to the server, exported and restored the MySQL database and updated the database connection details, but that's all I did.


Solution

  • use this

    'access' => [
       'class' => AccessControl::className(),
       'only' => ['logout', 'signup', 'create', 'edit','index','view','update','delete'],
       'rules' => [
           [
           'actions' => ['show'],
           'allow' => true,
                   'roles' => ['?'],
           ],
           [
           'actions' => ['index', 'logout', 'create', 'edit','view','update','delete'],
           'allow' => true,
           'roles' => ['@'],
           ],
      ],
    ],
    

    you need to specify the rule for those 'view','update','delete' otherwise you will not be able to access.