To check If a user is logged in I need to pull off a pretty long if-statement and then redirect the user depending if the user is logged in or not. I think a custom function like
if (logged_in()) { redirect }
Would be more appropriative. But building a library for one function seems unnecessary to me. What should I do?
It’s not at all “unnecessary”, neither is it strictly “necessary”, but it’s probably a good idea to create a library/class for this.
If you have a lot of logic you need to work with, “a pretty long if-statement” for example, using a class can help you break this down into smaller pieces and make the logic more manageable. If you only need to call one public method of the class, like
$this->auth->is_logged_in(), there’s nothing wrong with that, then you can create a small helper file or wrapper function to call the method, and put the redirect logic there instead of the class. Something like this perhaps:Using a class/library has many benefits, and with something as complicated as user authorization you will benefit greatly from taking advantage of it, especially once your project starts to expand and you need more utility.