CodeIgniter - How to check session to be used at every methods

前端 未结 3 1079
无人及你
无人及你 2021-01-05 09:27

Let say at my controller named Book, I have many methods, such as get_book(); read_book(); remove_book();

<
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 10:10

    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

        class Book extends Member_Controller {
    
            //Code that will be executed, no need to check anywhere if the user is logged in. 
            //The user is guaranteed to be logged in if we are executing code here.
    
    //If you define a __construct() here, remember to call parent::__construct();
        }
    

    This cuts code duplication a lot, since if you need another member controller other than Book you can just extend the Member_Controller. Instead of having to do the checks in all of them.

提交回复
热议问题