Welcome Guest, Not a member yet? Register   Sign In
Autorun php code?
#1

[eluser]Citizen[/eluser]
I'm new to CI and I'm making an app that has the login system built into the header of every page. The template header was easy but now I've got some php code that needs to run on almost every page.

Typically this would just mean using a php include on every page. Now that I'm using CI, is there a better or a best practice to handle such code?

Thanks,
-Ryan
#2

[eluser]jwburnside[/eluser]
I usually build a Base Controller for code that needs to be on each page or is vital to the site operations, and then extend that base controller in all of my other controllers.

For instance, since you have a login requirement, you will need an authentication system. This will go in your base controller since all users must pass the authentication to access you pages. Something along these lines:

Code:
$this->load->library('auth');
$this->id = $this->auth->username();

// Check for Authentication
if(!$this->auth->is_authenticated())
{
    //Redirect back to login
}
else
{
//Set the session or whatever
}
Technically this can go in each function in your main controller, but why repeat code! At any rate, it shouldn't go in your view, since that defeats the purposes of having a nice, ordered MVC structure.
#3

[eluser]Citizen[/eluser]
In CI, how do I create a base controller that all other pages extend?

Also, where is the standard place to put includes if I were to go that route?
#4

[eluser]jwburnside[/eluser]
A base controller could really go anywhere you want. I usually put mine in the controllers folder, and then build my apps in their own folders inside the controllers folder. To extend it, you need to require the file and update your class declaration:


Code:
require_once('/var/www/applications/controllers/base.php');
    
Class Airline_Controller extends Base_Controller {
...

You don't really have the need for "includes" in Codeigniter. You can just write code into the constructor, such as the authentication code. It will run automatically each time that you access any controller that has extended the base controller. If you have a specific function that you want to be available everywhere, you can make it into a helper, or a library if it is more complex.

You can also write functions in the base controller that can be called from the child controller. I use one called "render" that will take view paths, titles, styles, and scripts as parameters, an build the page out of them. Then in my child controller, instead of $this->load->view(), I would use $this->render().

Hope that helps.
#5

[eluser]InsiteFX[/eluser]
application/core/MY_Controller.php
Code:
class MY_Controller extends CI_Controller {

    // NOTE: Only needed if you need to intialize things!
    public function __construct()
    {
        parent::__construct();
    }

}
Then extend all your new controlers from MY_Controller.

InsiteFX
#6

[eluser]jwburnside[/eluser]
Ah yes, I forgot to mention that my Base controller extends CI_Controller!




Theme © iAndrew 2016 - Forum software by © MyBB