Welcome Guest, Not a member yet? Register   Sign In
Help: Where to put my controller function logged_in()

[eluser]Wondering Coder[/eluser]
I have this function in almost all of my controllers, this prohibit any unauthorized user to access a certain page in my site.
function is_logged_in()
        $is_logged_in = $this->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
            echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';    

But what I would like to do is just call this function logged_in in just one controller and not doing a copy-paste thing. Is this the part where I would need to extend my controller? Haven't really tried it though. BTW - It's just been 3 months since I learned using CI.

Read up on base controllers.

Phil Sturgeon wrote a good article about it.

In short, create an Public_Controller, Private_Controller and maybe an Admin_Controller. All extend CI_Controller. All your controllers that need to be accessable without authentication will extend Public_Controller, all controllers that require you to be logged in extend Private_Controller, which checks the logged in state. And you may have other base controller for specific functions.

You also use these base controllers to house all methods and properties that should be available to all your controllers, so you'll have to code them only once.

[eluser]Wondering Coder[/eluser]
thnx WanWizard,

I'm reading it right now. Actually I have 2 module site: Admin Module and Private Module and as for my public site maybe its just for my registration and login page.. I'm getting the picture now. I'm getting the picture now.

Also does this article up to date? Because I'm using CI 2.0.

[eluser]Wondering Coder[/eluser]
follow up question WanWizard,

this part confused me. Why do I need to save the Admin_Controller in the application/libraries not in the application/core just like the Public_Controller? Because I already extended my CI library to MY_Controller in the application/libraries. Or maybe I'm misunderstood something here?

CI doesn't have any class autoloading capabilities, so in theory you can put them where you want them.
You have to load them manually before the controller gets loaded.

As of CI 2.0, I would suggest putting them in core, together with MY_Controller. For CI 1.7.x, libraries is the place to store them.

[eluser]Wondering Coder[/eluser]
ahh, ok. Its just that based from what I understand all extended library in system/libraries had to be put in application/libraries. hmmn.

Anyway thanks a many WanWizard ^_^.

Theme © iAndrew 2016 - Forum software by © MyBB