Cakephp重定向给出错误调用null上的成员函数header()

时间:2015-05-06 03:55:38

标签: php cakephp

// awardscontroller

App::import('Controller', 'Permissions');
        class AwardsController extends AppController {
        public function add($id=NULL) {
            if (!$id) {
                throw new NotFoundException(__('Invalid User'));
            }
            if(!$this->Session->check('Auth.User')){
                $this->redirect(array('controller'=>'users','action' => 'login'));      
            }else{
                $Permission= new PermissionsController;
                if($Permission->isAuth($id)){
                    if ($this->request->is('post')) {
                        $this->Award->create();
                        $this->request->data['Award'][0]['basicprofile_id']=10;
                        var_dump($this->data);
                        if ($this->Award->saveAll($this->request->data['Award'])) {
                            $this->Session->setFlash(__('Award has been added '));
                            //$Permission= new PermissionsController;
                            //$this->redirect(array('controller'=>'users','action' => 'login'));
                            $Permission->redirectProfile($id);
                        } else {
                            $this->set('error',sizeof($this->request->data['Award']));
                            $this->Session->setFlash(__('The award has not added. Please, try again.'));
                        }   
                    }
                }else{
                    $this->Session->setFlash(__('You are not authorized to access it'));
                    $this->redirect(array('controller'=>'users','action' => 'login'));
                }
            }
        }
    }

// premissioncontroller

 <?php

        class PermissionsController extends AppController {


        public function redirectProfile($id){
            if(AuthComponent::user('role')=='Admin'){echo "working";
                $this->redirect(array('controller'=>'users','action' => 'index'));
            }else if(AuthComponent::user('role')=='Teacher'){
                //$this->redirect(array('controller'=>'profiles','action' => 'teacher', $id));
            }
        }
    }

    ?>

在permissioncontroller中,echo是打印但是url的重写给出了致命错误:在null上调用成员函数header()。任何Suggesstion

1 个答案:

答案 0 :(得分:0)

我想出了这个伎俩。 // awardscontroller

App::import('Controller', 'Permissions');
        class AwardsController extends AppController {
        public function add($id=NULL) {
            if (!$id) {
                throw new NotFoundException(__('Invalid User'));
            }
            if(!$this->Session->check('Auth.User')){
                $this->redirect(array('controller'=>'users','action' => 'login'));      
            }else{
                $Permission= new PermissionsController;
                if($Permission->isAuth($id)){
                    if ($this->request->is('post')) {
                        $this->Award->create();
                        $this->request->data['Award'][0]['basicprofile_id']=10;
                        var_dump($this->data);
                        if ($this->Award->saveAll($this->request->data['Award'])) {
                            $this->Session->setFlash(__('Award has been added '));
                            //$Permission= new PermissionsController;
                            //$this->redirect(array('controller'=>'users','action' => 'login'));
                            $this->redirect($Permission->redirectProfile($id));
                        } else {
                            $this->set('error',sizeof($this->request->data['Award']));
                            $this->Session->setFlash(__('The award has not added. Please, try again.'));
                        }   
                    }
                }else{
                    $this->Session->setFlash(__('You are not authorized to access it'));
                    $this->redirect(array('controller'=>'users','action' => 'login'));
                }
            }
        }
    }

// permissioncontroller

<?php

  class PermissionsController extends AppController {
public function redirectProfile($id){
            if(AuthComponent::user('role')=='Admin'){echo "working";
                return (array('controller'=>'users','action' => 'index'));
            }else if(AuthComponent::user('role')=='Teacher'){
                return (array('controller'=>'profiles','action' => 'teacher', $id));
            }
        }
    }

    ?>

只需返回url地址即可重定向并从awardscontroller本身重定向。