php - Cakephp 3 redirect not working if exception is after

899

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.

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

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.

This function is in 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));
    }

    ...
}
49

Answer

Solution:

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 toredirect is being ignored, "more code" is always executed.

This is not how the methodredirect is expected to be called, as mentioned in the docs (emphasis added):

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 ofCake\Controller\Controller::redirect() has been changed toController::redirect(string|array $url, int $status = null). The 3rd argument $exit has been dropped. The method can no longer send response and exit script, instead it returns a Response instance with appropriate headers set.

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
825

Answer

Solution:

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;

  }
}

People are also looking for solutions to the problem: php - trying to compare two email fields - page blanks out

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.