beforeFilter 中的 Cakephp3 重定向不起作用

Cakephp3 redirect in beforeFilter is not working

当我在控制器的 beforeFilter 中添加 $this->redirect 语句并在之后添加 return 时,重定向被忽略了。当我在控制器的动作中移动重定向语句时,它工作正常。有人知道我做错了什么吗?

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

由于我仅从 AppController 中的 beforeFilter 返回响应对象,因此重定向不起作用。 有关详细信息,请查看 https://github.com/cakephp/cakephp/issues/6705.

如果任何解决方案都不起作用,请执行以下代码。

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;
}