简体   繁体   English

如果异常发生,Cakephp 3重定向不起作用

[英]Cakephp 3 redirect not working if exception is after

I have an action in a controller where i call to a function that checks if a cookie is set, if the cookie is not set then it should redirect to somewhere else. 我在控制器中有一个动作,我调用一个函数检查是否设置了cookie,如果没有设置cookie,那么它应该重定向到其他地方。

In the same controller i have an evaluation for some variables, if they're not set then throw a forbidden exception but even when the cookie is not set it's not redirecting and the forbidden message appears 在同一个控制器中,我对某些变量进行了评估,如果它们没有设置则抛出一个禁止的异常,但即使没有设置cookie,它也不会重定向,并且会出现禁止的消息

The correct action should be redirect and never evaluate for the variables when the cookie doesn't exist or at least that's what i need. 正确的操作应该是重定向的,并且在cookie不存在时或者至少我需要的时候从不评估变量。

This function is in appController 这个函数在appController中

public function isCookieSet(){
  if(!$this->Cookie->check('cookie')){
    return $this->redirect($this->referer());
  }
}

And the code inside my controller 和我的控制器内的代码

public function editarImg($negocio_id=null){    

    $this->isCookieSet(); //should redirect here

    //but executes until here
    if((!isset($this->perRol['crear_negocios']) || $this->perRol['crear_negocios']==0) || 
      (!isset($this->perRol['cargar_elim_imagenes']) || $this->perRol['cargar_elim_imagenes']==0)){
      throw new ForbiddenException($this->getMensajeError(403));
    }

    ...
}

The code in the question can be rewritten (for clarity) like so: 问题中的代码可以重写(为清楚起见),如下所示:

public function editarImg($negocio_id=null){    
    if(!$this->Cookie->check('cookie')){
        $this->redirect($this->referer());
    }

    // more code

The return value of the call to redirect is being ignored, "more code" is always executed. 忽略对redirect的调用的返回值, 始终执行“更多代码”。

This is not how the method redirect is expected to be called, as mentioned in the docs (emphasis added): 这不是如何调用方法redirect ,如文档中所述 (强调添加):

The method will return the response instance with appropriate headers set. 该方法将返回具有适当标头集的响应实例。 You should return the response instance from your action to prevent view rendering and let the dispatcher handle actual redirection. 您应该从操作中返回响应实例以防止视图呈现,并让调度程序处理实际的重定向。

This is also mentioned in the migration guide : 迁移指南中也提到这一点:

The signature of Cake\\Controller\\Controller::redirect() has been changed to Controller::redirect(string|array $url, int $status = null) . Cake\\Controller\\Controller::redirect()的签名已更改为Controller::redirect(string|array $url, int $status = null) The 3rd argument $exit has been dropped. 第三个参数$ exit已被删除。 The method can no longer send response and exit script, instead it returns a Response instance with appropriate headers set. 该方法不再发送响应和退出脚本,而是返回具有适当标头集的Response实例。

Code example 代码示例

The code in the question needs to be the equivalent of this: 问题中的代码必须等同于:

public function editarImg($negocio_id=null){    
    if(!$this->Cookie->check('cookie')){
        return $this->redirect($this->referer()); # <- return the response object
    }

    // more code

Try this, it should work: 试试这个,它应该工作:

public function isCookieSet(){
  if(!$this->Cookie->check('cookie')){

    // return $this->redirect($this->referer()); <- not working

    $this->response = $this->redirect($this->referer());
    $this->response->send();
    exit;

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM