CodeIgniter Forums
Too Many Redirect error if the redirect call is on the same controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: Too Many Redirect error if the redirect call is on the same controller (/showthread.php?tid=90483)



Too Many Redirect error if the redirect call is on the same controller - elimariaaaa - 03-25-2024

I just noticed that when I call a redirect where the function is inside the same controller, it causes ERR_TOO_MANY_REDIRECTS error. However, when I call a redirect from another controller, it's working as expected.

Code:
public function _remap($method)
    {
        //code to redirect to login if not logged in
        
        if (auth()->loggedIn()) {
            $user = auth()->user();
            if($user->inGroup('admin','superadmin')){
                return redirect()->to('admin/users'); //this causes ERR_TOO_MANY_REDIRECTS
            } else {
                if($user->toRawArray()['is_paid'] == 0){
                    return redirect()->to('payment'); //this is okay
                } else {
                    return $this->{$method}($user->toRawArray());
                }
            }
        } else {
            return redirect()->to(config('Auth')->loginRedirect());
        }
    }



RE: Too Many Redirect error if the redirect call is on the same controller - kenjis - 03-25-2024

Yes, if you redirect to the same URL, you will go into infinite loop.


RE: Too Many Redirect error if the redirect call is on the same controller - elimariaaaa - 03-26-2024

So what should I do to stop the loop?

Code:
admin/users
is just:

Code:
public function usersView(){
        $users = auth()->getProvider();
        $user = auth()->user()->toRawArray();
        $data = array(
            'title' => 'Users',
            'users' => $users->where(['id !=' => $user['id']])->findAll() //$userModel->where(['id !=' => $user['id']])->findAll()
        );
        return view('admin/users', $data);
    }

I'm just expecting it to go to URL: admin/users and then show the view.


RE: Too Many Redirect error if the redirect call is on the same controller - kenjis - 03-26-2024

Sorry, I don't understand why do you need infinite loop redirects.