CodeIgniter Forums
redirect not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: redirect not working (/showthread.php?tid=92270)



redirect not working - 4usol - 01-03-2025

Hi i try to run a function from a library wich returns a redirect.

It failed, the redirect not fired, i dont know what i do wrong.

Here is the situation

in basecontroller i add my library on initController
Code:
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
    $this->mylib = new MyLib();  
}

also in basecontroller i defined these function
Code:
public function initPage(){
    $this->myLib = new MyLib();               //Why need to create a new instance and dont use the instance from initController - my tries failed - is it possible?
    $this->myLib->my_lib_function();
}


In the library i've two functions
- The one i've called in the base-controller
Code:
public function my_lib_function()
{
            $this->my_redirect('viewname');       //POS1    

            die("die after my_redirect");             //DIE1

           //go on...
}


and the function for handle and doing the redirect himself
Code:
public function my_redirect($view='defaultview')
{
        //header('Location: /Errors/'.$view);             //Here a redirect fired but thats not the way in codeigniter4 so i won't use it
        return redirect()->to('Errors::'.$view);            //Play around with "return" or without - has no effect
}

Notice: "Errors" is a controller in the main Controllers-Folder.
Here are the routes for them
Code:
$routes->get('errors/(:any)', 'Errors::code/$1');
  $routes->get('errors', 'Errors::index');

So the result is that the redirect from the POS1 dont run. Nothing happens.

If i add the "die(..)" statements the DIE1 print out, but not more. If i comment the DIE1 out, the code goes on...

Why the redirect dont work?


RE: redirect not working - InsiteFX - 01-03-2025

For one routes with (:any) need to be the last routes in the route list or they will not work.


RE: redirect not working - 4usol - 01-03-2025

(01-03-2025, 11:25 PM)InsiteFX Wrote: For one routes with (:any) need to be the last routes in the route list or they will not work.
Thats a interesting info, it it for a "any" rout for the same controller, so any routes for the same controller must defined at least of each?

I reduce it to only
Code:
"$routes->get('errors/(:any)', 'Errors::show/$1'); "
nothing happens...

But i think this is not the problem, if i put the url directly the route works...

In touch with my situation i find another point wich produces a same kind of problem, may here its easier to find the problem.

If i change the route for 404 errors in Routes.php
Code:
$routes->set404Override(function()
{
        //try to redirect
        //echo"URL: ".site_url('errors/show/error_404');

        header('Location: '.site_url('errors/error_404'));       

        // return redirect()->to('errors::show/'.$view); 
        // redirect()->to('errors::show/'.$view); 
        //die("");
}

The "header(..." works only if i add a "die(" command, if there is no "die" the header wont work. - Why?
The play arounds with "redirect()" - failed all - Why?

Also if i try this 
Code:
return redirect()->to('errors/error_404', null, 'refresh');

or
Code:
return redirect()->back()->withHeaders();
-> Error Object of class CodeIgniter\HTTP\RedirectResponse could not be converted to string
    on "if ($override instanceof Closure) {955                echo $override($e->getMessage());"


For me it only works by using the core "header(...)" function and "die()", i want to understand why the "redirect" dont work, so i hope someone can find the missing info:-)