![]() |
Redirect inside Constructor NOT WORKING - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Redirect inside Constructor NOT WORKING (/showthread.php?tid=74537) Pages:
1
2
|
Redirect inside Constructor NOT WORKING - Poetawd - 10-06-2019 Hello All, I need help with a explanation on WHY a "return redirect->to()" wont work inside a constructor... This wont work: PHP Code: public function __construct() This will work: PHP Code: public function __construct() Of course this is only a example and I will have some logic inside the constructor, like: if(NotLogged){ redirect() } Thank you for the explanation... RE: Redirect inside Constructor NOT WORKING - dave friend - 10-06-2019 It doesn't work because there isn't any code that is looking for a return from the constructor. As a rule, PHP __construct() methods should not return anything. RE: Redirect inside Constructor NOT WORKING - kilishan - 10-06-2019 Redirect now returns a class instead of just setting headers. You cannot return an instance of another class while instantiating a different class in PHP. Check out Controller Filters RE: Redirect inside Constructor NOT WORKING - Poetawd - 10-07-2019 (10-06-2019, 08:42 PM)kilishan Wrote: Redirect now returns a class instead of just setting headers. You cannot return an instance of another class while instantiating a different class in PHP. Thank you Sir ! I am sooo used to use CI3 that I forgot a little bit about the PHP itself... I am very eager to use Myth Auth in my projects... But, meanwhile, I am using Ben´s Ion Auth 4 ... The point is that I want to make my code cleaner and I am checking for login in the constructor or in the baseController. Do you have any tips about the best way of doing it ? I am doing like this: PHP Code: public function checkLoggedIn() RE: Redirect inside Constructor NOT WORKING - RobT - 12-02-2019 Hi, I would like to know how to redirect in constructor too for "code cleaniness" as OP said. Thanks for your help. RE: Redirect inside Constructor NOT WORKING - kilishan - 12-02-2019 "code cleanness" can mean any number of things. For many of those definitions, Controller Filters, as previously mentioned, will keep it clean for you. If you absolutely must do it from the constructor, you can do a slightly hacky thing and throw a RedirectException with the URL to redirect to as the message. But I highly encourage looking into controller filters, as they're specifically designed to handle the types of situations you're asking about. RE: Redirect inside Constructor NOT WORKING - Poetawd - 12-02-2019 (12-02-2019, 10:15 AM)kilishan Wrote: "code cleanness" can mean any number of things. For many of those definitions, Controller Filters, as previously mentioned, will keep it clean for you. You can always redirect using the "vanilla" PHP way: PHP Code: //Redirects to Unauthorized main page Or send a JSON message and stop php after: PHP Code: //Return a JSON response with the error ![]() That is how I did it inside construct... RE: Redirect inside Constructor NOT WORKING - Avega Soft - 12-24-2019 (12-02-2019, 10:15 AM)kilishan Wrote: "code cleanness" can mean any number of things. For many of those definitions, Controller Filters, as previously mentioned, will keep it clean for you. Thanks a lot Lonnie for this advice/comment and this tutorial (https://www.patreon.com/lonnieezell/posts?tag=filters) it's very helped me when catch similar problem with redirect. RE: Redirect inside Constructor NOT WORKING - InsiteFX - 12-24-2019 The correct way of doing it in CI 4 is using Controller Filters. This is how Lonnie doe's it for Myth/Auth. PHP Code: <?php namespace Myth\Auth\Filters; Would be a good idea to sit down on a weekend and read the CodeIgniter 4 User Guide. RE: Redirect inside Constructor NOT WORKING - littlej - 01-24-2020 It might interest someone... you can still redirect from the constructor using: PHP Code: header('Location: '.base_url()); |