本文介绍了获取“Headers already sent"在 beforeAction 中渲染视图时 Yii 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了 Yii2 的处理程序,但我不明白如何在这种情况下正确使用它们.

基本上在我的 SiteController 中,我有:

class SiteController extends \app\components\Controller{公共函数 beforeAction($action){//进行一些检查,如果为真,将渲染一个文件并停止执行任何操作如果 (...)echo $this->render('standby');返回假;}返回真;}//我所有的其他动作都在这里}

这似乎运行良好并停止了执行,但是我得到了 render() 行的Headers already sent",就好像它在做重定向一样.

如果我写 Yii::$app-end() 而不是 return false,同样的事情会发生.

如果我写 exit(); 而不是 return false,没有异常显示但调试面板没有显示,因为 Yii 没有正确终止.>

我尝试删除 echo $this->render(..) 并导致一个空页面,没有任何重定向,这似乎只是 Yii 抱怨我从控制器回显内容.

当然我不能返回 render() 的结果或返回 true,因为它会执行页面的动作,我试图避免并到此结束.

我知道在 beforeAction() 中返回 false 会触发 EVENT_BEFORE_ACTION 但我不知道我应该在哪里使用它.事件文档并没有真正帮助我.

那么有没有办法显示待机"视图,阻止其他动作执行并避免从Controller回显错误信息?

请注意,我正在尝试使这项工作无需在每个操作方法中复制代码来检查 beforeAction() 的结果是否为假.

解决方案

自 Yii 2.0.14 起,您无法在控制器中回显 - 响应必须由操作返回.如果你想在 beforeAction() 中生成响应,你需要设置 Yii::$app->response 组件而不是回显内容:

public function beforeAction($action) {//进行一些检查,如果为真,将渲染一个文件并停止执行任何操作如果 (...) {Yii::$app->response->content = $this->render('standby');Yii::$app->response->statusCode = 403;//这里使用真实的 HTTP 状态码返回假;}return parent::beforeAction($action);}

不要忘记调用 parent::beforeAction($action) - 省略它会导致意外且难以调试的行为.

I've read about Yii2's handlers and I don't grasp how to use them properly for this case.

Basically in my SiteController, I have:

class SiteController extends \app\components\Controller
{
    public function beforeAction($action)
    {
        // Makes some checks and if it's true, will render a file and stop execution of any action
        if (...)
            echo $this->render('standby');
            return false;
        }
        return true;
    }

    // All my other actions here
}

This seems to work well and stop the execution, however I get the "Headers already sent" for the render() line, as if it was doing a redirection.

If I write Yii::$app-end() instead of return false, same thing happens.

If I write exit(); instead of return false, no exception shows but the debug panel doesn't show since Yii doesn't terminate correctly.

I tried removing echo $this->render(..) and it results in an empty page, no redirection whatsoever, this seems like it's only Yii complaining that I echo stuff from the Controller.

And of course I can't return the result of render() or return true, since it will execute the page's action, which I'm trying to avoid and end here.

I know returning false in beforeAction() triggers EVENT_BEFORE_ACTION but I don't see where I'm supposed to use that. The events documentation didn't really help me.

So is there a way to display the "standby" view, preventing other actions from executing and avoid the error message for echoing from the Controller?

Please note that I'm trying to make this work without having to duplicate code in each action method to check if the result of beforeAction() was false.

解决方案

Since Yii 2.0.14 you cannot echo in controller - response must be returned by action. If you want to generate response in beforeAction() you need to setup Yii::$app->response component instead of echoing content:

public function beforeAction($action) {
    // Makes some checks and if it's true, will render a file and stop execution of any action
    if (...) {
        Yii::$app->response->content = $this->render('standby');
        Yii::$app->response->statusCode = 403; // use real HTTP status code here

        return false;
    }

    return parent::beforeAction($action);
}

Don't forget to call parent::beforeAction($action) - omitting it will result unexpected and hard to debug behavior.

这篇关于获取“Headers already sent"在 beforeAction 中渲染视图时 Yii 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 21:15