klickagent.ch
klickagent.ch

Reputation: 559

Cakephp3 redirect in beforeFilter is not working

When I add the $this->redirect statement inside a beforeFilter of a controller and return afterwards, the redirection is being ignored. When I move the redirect statement inside an action of the controller, it works fine. Does someone has an idea what I am doing wrong?

public function beforeFilter(Event $event) {
    if( true ){ //if a sample condition is true
        return $this->redirect([
            'controller' => 'test',
            'action' => 'action'
        ]);
    }

Upvotes: 2

Views: 1551

Answers (2)

Chetan Varshney
Chetan Varshney

Reputation: 666

If anything solution does not work do following code.

public function beforeFilter(Event $event) {
if( true ){ //if a sample condition is true
    $url = '/'; //set url here
    echo '<script type="text/javascript">';
        echo 'window.location="'.$url.'"';
    echo '</script>';
    exit;
}

Upvotes: -1

klickagent.ch
klickagent.ch

Reputation: 559

Since I was returning the response object only from the beforeFilter in the AppController the redirect did not work. For further details check out https://github.com/cakephp/cakephp/issues/6705.

Upvotes: 2

Related Questions