0

Whenever I include the beforeAction function event with something simple I get an error saying it has to be compatible. This does not happen on my live server only on my local one. The only difference I can think of is my local server is running PHP7 and my live one is running PHP5.6. Is that what is causing the issue? The only thing I have found that will fix it is removing it completely.

This is what my beforeAction looks like

 public function beforeAction()
    { 
        if(Yii::$app->user->isGuest){
            return $this->redirect(['site/login']);
        } else {
            if(strtotime(UserInfo::findOne(Yii::$app->user->Id)->active_until) < strtotime(date("Y-m-d H:i:s"))){
                Yii::$app->session->setFlash('warning', 'You need an active subscription to access events.');
                echo("<script>location.href = '".Url::toRoute('site/subscription')."';</script>");
                exit;
                //return $this->redirect(['site/subscription']);
            }else {
                return true;
            }
        }
    }

I also tried this simple one to check and got the same issue

public function beforeAction()
    {
        if (!parent::beforeAction($action)) {
            return false;
        }
        return true;
    }

Here is the error message I get

Declaration of frontend\controllers\EventController::beforeAction() should be compatible with yii\web\Controller::beforeAction($action)
Michael St Clair
  • 5,937
  • 10
  • 49
  • 75

2 Answers2

3

See this error message :

should be compatible with yii\web\Controller::beforeAction($action)

Your override function must be compatible with parent. So, valid code :

public function beforeAction($action)
    { 
        ....
    }
Ivoglent Nguyen
  • 504
  • 3
  • 9
1

Just explaining

If you get this problem, it means that you have a function in your parent class and a function in your child class that have the same name but not the same input variable declaration.

Parent Class

beforeAction($action) {
....
}

Child Class

beforeAction() {
....
}

As you can see, the child class is missing the $action variable. This creates a warning in PHP E_Strict. All you have to do is make sure that the child class function is exactly the same as the parent.

Lionel Yeo
  • 383
  • 4
  • 13