CodeIgniter Forums

Full Version: controller redirect to method fails
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings,  So far I like the concepts and implementation of CI4 - great job!  
However, I can't get the CI3 redirect equivalents to work in CI4.  I'm using the CI4 Beta 4.0.0 - 1, composer installed.

Desired function: Controller needs to pass control to another controller (or method, module, service, etc).  
Purpose: I created a login module because I have 4 applications using similar code.  I will use routes to return control, and encrypted cookies for validations. BUT, perhaps there may be a bug somewhere.  The code below seems too simple to fail.
Minimalist failing code:
- I added the route: $routes->get('/(:any)', 'Home::$1');
- Modified the canned Home controller to add a dummy method
- Directly calling http://localhost/dummy works just fine.
- redirect ('dummy'); fails, as do all derivative approaches like: route_to('dummy'), header ("location: dummy"); specifying namespaces and home controller ("app\home\dummy"), etc. all fail...

Code follows.  Help anyone??

config/routes:  $routes->get('/(:any)', 'Home::$1');  (and also tried ...->add and other hacks to no avail)

<?php namespace App\Controllers;
use CodeIgniter\Controller;
class Home extends Controller {

public function index() {
  redirect ("dummy");
  echo "didn't route.";
  //return view('welcome_message');

public function dummy() {
 echo "Dummy route.";

- Thanks
Try redirect('/dummy');
(03-12-2019, 11:41 AM)donpwinston Wrote: [ -> ]Try redirect('/dummy');

Thanks, I did that and all variations of locations and namespaces, etc.

It seems the correct answer is:  
return redirect()->to('ci4/dummy');    // ('ci4' is my new "public" directory where index.php resides)

I found the answer in another post:

Note that ":redirect ('ci4/dummy');" will also fail.  The reason I mentioned it as a bug is because the fail is:
SYSTEMPATH/HTTP\Exceptions\HTTPException.php at line 106

Thanks all!
can you try $this->response->redirect( 'dummy' ); ?

had also some problems with the redirect helper, but the response function works great for me:
You have to return any redirects. Simply calling redirect doesn't work.

public function index() {
    return redirect ("dummy");
Following up: Full code examples below of what worked and didn't...

Thanks for all the suggestions.  Most failures end with a CI4 code exception: SYSTEMPATH/HTTP\Exceptions\HTTPException.php at line 106

The root problem for me is simply vague documentation about reverse routing.  I've just no desire to poor through the CI4 system code.
I should mention I'm writing modules in different directories and namespaces with a "global" config, so I needed a general solution to redirect routing, etc...

PHP Code:
<?php namespace App\Controllers;
Home extends Controller
    // Just jump to a different controller...  
        // Remember to add the route: $routes->get('/(:any)', 'Home::$1');
public function index()
// in CI3:  redirect('dummy');  // traditional.

               // in CI4:
return redirect()->tobase_url('dummy') ); // this works (preferred solution I think)

        //return redirect()->to('ci4/dummy'));         // this works (/ci4 is my new "public" dir)
        //$this->response->redirect('dummy');       // this also works
                //$this->response->redirect(base_url('dummy')); // this version also works

        // return redirect("dummy");              // fails with CI code exception
        // return redirect(base_url('dummy'));    // fails with CI code exception
        // return redirect('App/Controller/Home::dummy');    // fails with CI code exception

public function dummy()
"Dummy route.";