![]() |
Redirect to a different page from Library class in CI4 - 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 to a different page from Library class in CI4 (/showthread.php?tid=92396) |
Redirect to a different page from Library class in CI4 - serjik - 01-29-2025 I would like to make a redirect from Library class in CI4. It does not work. I am changing the code from CI3 to CI4 and after I read a lot of topics regarding redirect I could not find an example for my case. In CI3 it was easy to use redirect(): PHP Code: redirect(base_url($redirect_url), 'location', 301); PHP Code: return redirect()->to(base_url($redirect_url)); I am getting an error if I use redirect()->route(): The route for URL cannot be found. PHP Code: return redirect()->route($redirect_url,[], 301); This is not from Controller. I need a redirect to a different page from Library Class, which is located in APP\Libraries\LibraryFolder\LibraryClass.php Please let me know if there is a way to make it work. RE: Redirect to a different page from Library class in CI4 - InsiteFX - 01-29-2025 This works in a html view may work in a library not sure or tested. PHP Code: // Try to use a named route. |name RE: Redirect to a different page from Library class in CI4 - serjik - 01-30-2025 (01-29-2025, 11:50 PM)InsiteFX Wrote: This works in a html view may work in a library not sure or tested. No, this redirect does not work from the Library class. I tried to use header() and it works only with exit() PHP Code: header('Location: '. base_url($redirect_url)); I created, as a temporary solution, a function in helper: PHP Code: function redirect_to_page($page) { I am using it in the library class: PHP Code: redirect_to_page(base_url($redirect_url)); It would be better to use CodeIgniter methods, but so far I could not find a solution. |