Let say at my controller named Book, I have many methods, such as
get_book(); read_book(); remove_book();
No methods in the class can be used without user logged in, and I can get the user_id from session.
My question is, what is/are the best ways to check the if the user_id session is set so that I can use the methods?
As for now I am thinking of creating a is_logged_in() method, and apply it to every methods with an if-else statement, like
if($this->is_logged_in()
{
//do something
}
else
{
//redirect to home
}
Isn’t it long and tedious? Is there an ultimate way to achieve this?
I read the link
codeigniter check for user session in every controller
But it seems that I still have to apply the is_logged_in check at every methods.
Thank you for helping me!
Create a file called
MY_controller.php(the prefix can be edited in config file) in/application/core:Then anytime you create a new controller, you decide what access it requires
This cuts code duplication a lot, since if you need another member controller other than
Bookyou can just extend theMember_Controller. Instead of having to do the checks in all of them.