Welcome Guest, Not a member yet? Register   Sign In
Check at the start of controller...???
#1

[eluser]Gewa[/eluser]
Hi, during programming in CI, i make user role check in each function for one controller. And there is a lot of things that I want to have in my hand beforehand in each controller before the function index()..


So what is the best way, for example to make


Code:
if($variable!="value"){
header("Location: /customer/login");
}


this code before the functions part,


instead of putting this code in all functions.....
#2

[eluser]James Gifford[/eluser]
If you want the code to execute at the start of every page load you can put it in the constructor of each of your controllers or you can create a custom parent controller and put the code in its constructor then have each of your page controllers extend this custom controller.
#3

[eluser]Gewa[/eluser]
You mean like this?



Code:
class Example extends Controller {

       function Example()
       {
            parent::Controller();
                          
          
    SOME CODE HERE ;
      }
#4

[eluser]James Gifford[/eluser]
That's one way. If you do that, the code will execute with each page load for that controller only. If you want the code to execute for all page loads (all controllers) you can make a controller class in your libraries folder like this:

Code:
class MY_Controller extends Controller
{
    function MY_Controller ()
    {
        parent::Controller();
        
        // Your code here
    }
}

then simply extend this class in all your page controllers:

Code:
class Example extends MY_Controller
{
    function Example ()
    {
        parent::Controller();
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB