Cakephp redirect对null上的成员函数header()进行错误调用
//获奖者控制者Cakephp redirect对null上的成员函数header()进行错误调用,php,cakephp,Php,Cakephp,//获奖者控制者 App::import('Controller', 'Permissions'); class AwardsController extends AppController { public function add($id=NULL) { if (!$id) { throw new NotFoundException(__('Invalid User')); }
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'));
}
}
}
}
//预发控器
<?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));
}
}
}
?>
<?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));
}
}
}
?>
在permissioncontroller中,echo正在打印,但url的重拨会产生致命错误:调用null上的成员函数header()。任何建议我想出了窍门。
//获奖者控制者
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'));
}
}
}
}
//许可证控制员
<?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));
}
}
}
?>
<?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本身重定向它。它是否打印“正在工作”?是的,如果$this->redirect(数组('controller'=>'users','action'=>'index'),它的打印工作正常;请告诉我们,如果您将
if(AuthComponent::user('role')=='Admin'){echo“正在工作”;$this->redirect(数组('controller'=>'users','action'=>'index'));}放入其他if(AuthComponent::user('role')=='Teacher'){/$this->redirect(数组('controller'=>'profiles','action'=>'Teacher',$id))会发生什么;}
code而不是$Permission->redirectProfile($id)代码>?我的意思是在当前控制器函数本身中替换它。简言之,您必须调用函数,而不是再次编写相同的代码。简单和简短:)正确吗?我想这是cakephp的问题。因此,只需返回要重定向的url地址,并从awardscontroller本身重定向它即可。:)。