Redirect inside Constructor NOT WORKING |
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...
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.
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 (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()
Hi,
I would like to know how to redirect in constructor too for "code cleaniness" as OP said. Thanks for your help.
"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. (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...
(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.
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. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
It might interest someone... you can still redirect from the constructor using:
PHP Code: header('Location: '.base_url()); |
Welcome Guest, Not a member yet? Register Sign In |