Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Problem with response redirect's return way

In my program, I use this code to show information with users' operation:

    public function infoShowWithRedirect($msg, $redirectRouteName = 'homepage', $waitTime = 3, $msgType = "success") {
        switch ($msgType) {
            case 'success':
                $this->flashSession->success($msg);
                break;
            case 'error':
                $this->flashSession->error($msg);
                break;
            default:
                $this->flashSession->notice($msg);
                break;
        }

        return $this->response->redirect(array(
            'for' => 'user_notice',
            'routeName' => $redirectRouteName,
            'waitTime' => $waitTime
        ));
    }

And in my controller base, I use previous code this way:

            // Get the current identity
            $identity = $this->auth->getIdentity();
            // If user is not login then redirect the user to login page
            if (!is_array($identity)) {
                echo 'execute';
                return $this->helper->infoShowWithRedirect(
                    $this->lang->_('has_no_privilege_login'),
                    'user_login',
                    2,
                    'error'
                );
            }

But I found that it echod "execute" but not redirect to user_login route, so I modify the infoShowWithRedirect() function:

        $this->response->redirect(array(
            'for' => 'user_notice',
            'routeName' => $redirectRouteName,
            'waitTime' => $waitTime
        ));
        $this->view->disable();
        return false;

And now it works, Why can't I use direct return redirect ?

B.T.W.

        $this->response->redirect(array(
            'for' => 'user_notice',
            'routeName' => $redirectRouteName,
            'waitTime' => $waitTime
        ));
        $this->view->disable();
        return ;

Is also not working!



50.7k
Accepted
answer

Returning the redirect doesn't stop the view from rendering. If you call $this->view->disable() in your infoShowWithRedirect() method, before you return, then it should work.