Welcome Guest, Not a member yet? Register   Sign In
controller redirect to method fails
#1

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.
Huh
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
Reply
#2

Try redirect('/dummy');
Simpler is always better
Reply
#3

(03-12-2019, 10: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:   https://forum.codeigniter.com/thread-72681.html

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!
Reply
#4

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

had also some problems with the redirect helper, but the response function works great for me:
Reply
#5

You have to return any redirects. Simply calling redirect doesn't work.

Code:
public function index() {
    return redirect ("dummy");
}
Reply
#6

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;
use 
CodeIgniter\Controller;
class 
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()
    {
        echo 
"Dummy route.";
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB