【问题标题】:Yii2 Declaration beforeAction should be compatible with ControllerYii2 声明 beforeAction 应该与 Controller 兼容
【发布时间】:2016-03-18 00:17:33
【问题描述】:

每当我将beforeAction 函数事件包含在一些简单的东西中时,我都会收到一条错误消息,指出它必须兼容。这不会仅在我的本地服务器上发生在我的实时服务器上。我能想到的唯一区别是我的本地服务器运行的是 PHP7,而我的实时服务器运行的是 PHP5.6。这是造成问题的原因吗?我发现唯一能修复它的就是完全删除它。

这就是我的beforeAction 的样子

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

我也试过这个简单的检查并遇到同样的问题

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

这是我收到的错误消息

Declaration of frontend\controllers\EventController::beforeAction() should be compatible with yii\web\Controller::beforeAction($action)

【问题讨论】:

  • 试过这个吗?返回父级::beforeAction($action);
  • 是的,我也试过了
  • 希望这会有所帮助.. 没主意了哈哈。 stackoverflow.com/questions/4017193/…
  • 刚刚尝试过,仍然出现该错误

标签: php yii2


【解决方案1】:

看到这个错误信息:

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

您的覆盖函数必须与父函数兼容。所以,有效的代码:

public function beforeAction($action)
    { 
        ....
    }

【讨论】:

  • 一开始我并没有看到它有什么不同,呃只是省略了$action。感谢您的帮助
【解决方案2】:

只是解释

如果遇到这个问题,说明你的父类中有一个函数,你的子类中有一个函数同名但输入变量声明不同。

父类

beforeAction($action) {
....
}

儿童班

beforeAction() {
....
}

如您所见,子类缺少 $action 变量。这会在 PHP E_Strict 中创建一个警告。您所要做的就是确保子类函数与父类函数完全相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 2011-04-30
    • 2017-11-07
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多