CodeIgniter4: Issues with redirects - had to use exit to make it work and blank page


name: Bug report about: Help us improve the framework by reporting bugs!


Describe the bug There are two issues with the redirect() method.

  1. I am not able to use return redirect()->to('/'); so I have to follow it up with an exit to make it work. e.g.
public function hello() {
//this won't work;
return redirect()->to('/');
//this works
redirect()->to('/');
exit;
}
  1. The other issue is none of the redirects are working now since approximately after yesterday (17 nov 2018) evening. To make sure, I haven’t accidently broken it, I just downloaded a new copy of CI4-develop and after setup I could see welcome message to confirm everything working fine. Then I made these modifications below to the Home controller. If I try mylocalsite/home/daljit it just shows a blank page.
 namespace App\Controllers;

use CodeIgniter\Controller;

class Home extends Controller
{
	public function index()
	{
		return view('welcome_message');
	}

	//--------------------------------------------------------------------
    public function daljit()
    {
        return redirect()->to("/home/hello");
    }

    public function hello()
    {
        echo "Hello!";
    }
}

CodeIgniter 4 version CodeIgniter 4 develop branch.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 50 (26 by maintainers)

Most upvoted comments

@lonnieezell yes that makes sense however please take a look at this test below …

It doesn’t redirect to login but just displays the text “You are here because you are logged in”. That’s incorrect. It should have redirected to /home/login.

The other tests 1 to 5 above still seem to work OK so can’t figure out what’s going wrong with this one.

I tested this is on fresh installation which I downloaded few mins ago.

namespace App\Controllers;

use CodeIgniter\Controller;

class Home extends Controller
{
    public function test6() {
		redirectIfNotLoggedIn();
		echo "You are here because you are logged in!";
    }
   public function login() {
		echo "You must login first!";
    }
}

///---------------- Some helpers -----------------------------------//
function redirectIfNotLoggedIn($refUrl=null) {
    if($refUrl == null) {
        $refUrl = currentUrlWithQueryString();
    }
    if(isUserLoggedIn() == false) {
        return redirect()->to('/home/login/?ref='.$refUrl);
    }
}
function isUserLoggedIn() {
    if (session('loggedin') == true) {
        return true;
    }
    return false;
}
function currentUrlWithQueryString($includeBaseUrl=false) {
    $uri = $_SERVER['REQUEST_URI'];
    if($includeBaseUrl == true) {
        $uri = base_url($uri);
    }
    return $uri;
}