Yii 错误:无法修改标头信息 - 标头已由 [英] Yii Error: Cannot modify header information - headers already sent by

查看:33
本文介绍了Yii 错误:无法修改标头信息 - 标头已由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是个伊比人.我想要做的是获取注册用户的角色并根据他们的角色向他们展示内容.我现在有两个用户

im a yiibie. what i am trying to do is to get the role of the registered users and show them the content according to their role. i have two users right now

  1. 管理员

用户(已认证)

我正在做的是,当我的管理员通过 user/login 登录时,管理员被重定向到一个页面,该页面是 "webapp/story" 和一个侧边栏小部件显示给我已经创建的管理员,当用户(经过身份验证)通过 "user/login" 登录时,该用户将被重定向到 index.php.使用代码执行此操作后,当我的管理员被记录时,它会显示错误:

What i am doing is that when my admin logs in via user/login the admin is redirected to the a page which is "webapp/story" and a sidebarwidget is shown to the admin which i have already made and when the user(authenticated) gets logged in via "user/login" that user is just redirected to index.php. after using the code to do this, when my admin gets logged it shows the error:

"无法修改标头信息 - 标头已由(输出开始于C:\wamp\www\emergency_response\protected\components\views\admin.php:73)"

"Cannot modify header information - headers already sent by (output started at C:\wamp\www\emergency_response\protected\components\views\admin.php:73) "

我使用了下面给出的代码,并在 modules/user/controller/logincontroller.php 中使用了此代码.

I have used the code which is given below in and used this code in modules/user/controller/logincontroller.php.

<?php

class LoginController extends Controller
{
public $defaultAction = 'login';

/**
 * Displays the login page
 */
public function actionLogin()
{
    if (Yii::app()->user->isGuest) {
        $model=new UserLogin;
        // collect user input data
        if(isset($_POST['UserLogin']))
        {
            $model->attributes=$_POST['UserLogin'];
            // validate user input and redirect to previous page if valid
            if($model->validate()) {
                $this->lastViset();
  //this is the code which i have used for the task i am doing.
                $role = Rights::getAssignedRoles(Yii::app() -> user     ->     Id);
          foreach ($role as $role)
            $role -> name;
        if ($role -> name == 'Admin'){
            $this->widget('AdminWidget') &&     $this->redirect(array('story'));
        }
        else{
            $this->redirect(Yii::app()->user->returnUrl);
        }
            }
        }
        // display the login form
        $this->render('/user/login',array('model'=>$model));
    } else
        $this->redirect(Yii::app()->controller->module->returnUrl);
    }

private function lastViset() {
    $lastVisit =     User::model()->notsafe()->findByPk(Yii::app()->user->id);
    $lastVisit->lastvisit = time();
    $lastVisit->save();
    }

}

推荐答案

你可以先试试简单的:

替换:

$this->widget('AdminWidget') && $this->redirect(array('story'));

致:

$this->widget('AdminWidget');
$this->redirect(array('story'));

但我不认为上述方法会起作用,因为您将进行重定向并且 $this->widget('AdminWidget'); 的第一条语句在重定向.

But i don't think that the above will work because you will have a redirect and the first statement of $this->widget('AdminWidget'); will not take effect after the redirect.

第二个想法是进行一些更改,因为我认为您有逻辑错误.试试这个:

Second thought is to make some changes because i think you had logic errors. Try this:

class LoginController extends Controller {
    public $defaultAction = 'login';

    /**
     * Displays the login page
     */
    public function actionLogin() {
        if (Yii::app()->user->isGuest) {
            $model = new UserLogin;
            // collect user input data
            if (isset($_POST['UserLogin'])) {
                $model->attributes=$_POST['UserLogin'];
                // validate user input and redirect to previous page if valid
                if ($model->validate()) {
                    $this->lastViset();
                    //this is the code which i have used for the task i am doing.
                    $role = Rights::getAssignedRoles(Yii::app()->user->Id);
                    foreach($role as $role) {   //Loop every role
                        if ($role->name == 'Admin'){    //If a role is Admin then redirect
                            $this->redirect(array('webapp/story'));
                        }
                    }
                    //If none of the roles were admin then redirect to a user view
                    $this->redirect(Yii::app()->user->returnUrl);
                }
            }
            // display the login form
            $this->render('/user/login', array('model'=>$model));
        } else {
            $this->redirect(Yii::app()->controller->module->returnUrl);
        }
    }

    private function lastViset() {
        $lastVisit =     User::model()->notsafe()->findByPk(Yii::app()->user->id);
        $lastVisit->lastvisit = time();
        $lastVisit->save();
    }

}

WebappController 中:

class WebappController extends Controller {

    public function actionStory() {
        $this->widget('AdminWidget');   //Maby you have to add this inside the view
        //Other code
        $this->render('webapp');
    }
}

这篇关于Yii 错误:无法修改标头信息 - 标头已由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆