RSS

Building My Class Website (Day 3 | Setting Up the Base Controllers)

25 May

Ok so we’re done with configuring CodeIgniter and setting up the environment and databse.

What’s next? Setting Up the Base Controllers.

Why? Because we want to set some settings for some page. For instance, in the admin page we need to check if a user is logged in. We will make use of base controllers and extend it so as to set control to some pages that needs it.

Task 3: Setting Up the Base Controllers

Step 1: Create a Custom Controller that extends the default Base Controllers

To start, let’s create a php file and name it MY_Controller and store it in the xampp>htdocs>application>core folder because this is where the default controller lives:

Setting Up Base Controller 1.PNG

Now let’s type in the following script on our file:

<?php
    class MY_Controller extends CI_Controller
    {
        function __construct(){
        parent::__construct();
    }
}

Setting Up Base Controller 2.PNG

What each line means:

  • class MY_Controller extends CI_Controller
    • our custom controller will inherit all the features of the CI_Controller
  • public $data = array();
    • create a public variable data that will hold an empty array
  • function __construct()
    • create a constructor because this is where we will place our code that needs to be run on every page
  • parent::__construct();
    • call the parent constructor
  •  $this->data[‘errors’] = array();
    • add a case that would have this data’s errors which would have an empty array
  •  $this->data[‘site_name’] = config_item(‘site_name’);
    • and data’s sitename which would be all the sitename that we configured in our config file

Now let’s check if our code works.

To check, let’s use our custom controller by plugging it in the welcome.php file located at xampp>htdocs>application>controllers folder:

Setting Up Base Controller 3.PNG

Setting Up Base Controller 4.PNG

Now, le’ts plug in the following script to the Welcome file:

class Welcome extends MY_Controller
var_dump($this->data);

Setting Up Base Controller 5.PNG

What each line means:

  • class Welcome extends MY_Controller
    • we replaced the default controller with our custom controller named MY_Controller
  • var_dump($this->data);
    • output the values returned by $this->data

Now, let’s save it and refresh our webpage to check if it works:

Setting Up Base Controller 6.PNG

And yes it did. 🙂

Now, since we already have a custom controller, maybe it would be better if we add separate controllers for frontend and backend part.

So the next thing that we will do is to create frontend and backend controllers.

Step 2: Creating Front End and Back End Controllers

What we want to do:

  • front end controller – display menu on all pages
  • back end controller – create a login check

To start, let’s create a php file and name it Frontend_Controller.php and type in the following script:

class Frontend_Controller extends MY_Controller
{
    function __construct(){
        parent::__construct();
    }
}

Setting Up Base Controller 7.PNG

Save the file at xampp>htdocs>application>libraries folder:

Setting Up Base Controller 8.PNG

What each line means:

  • class Frontend_Controller extends MY_Controller
    • create a Frontend_Controller class  that extends our custom controller MY_Controller
  • function __construct()
    • create a new constructor
  • parent::__construct();
    • call the constructor parent

Next, let’s create another php file again and name it Admin_Controller.php.

Type in the following script:

class Admin_Controller extends MY_Controller
{
    function __construct(){
        parent::__construct();
    }
}

Setting Up Base Controller 9.PNG

What each line means:

  • class Admin_Controller extends MY_Controller
    • create a Admin_Controller class  that extends our custom controller MY_Controller
  • function __construct()
    • create a new constructor
  • parent::__construct();
    • call the constructor parent

Save the file at xampp>htdocs>application>libraries folder:

Setting Up Base Controller 10.PNG

Next, we will now use the Frontend_Controller in our welcome page.

To do that, let’s open up again welcome.php from xampp>htdocs>application>controllers folder and change MY_Controller to Frontend_Controller:

Setting Up Base Controller 11.PNG

Now., let’s refresh again the page to check if it works:

Setting Up Base Controller 12

And it doesn’t seem to work. 😦

Why? Because the Frontend controller cannot be found by the codeigniter.

Solution: Add an autoloader for frontend controller.

To do that, open the config file located at xampp>htdocs>application>config> folder:

Setting Up Base Controller 13.PNG

Then type it the following script:

function __autoload($classname){
   if(strpos($classname, 'CI_') !== 0){
       $file = APPPATH . 'libraries/' . $classname . '.php';
       if (file_exists($file) && is_file($file)) {
           @include_once($file);
       }
    }
}

Setting Up Base Controller 14.PNG

What each line means:

  • function __autoload($classname)
    • declare an autoload function that takes the classname as an argument
  • if(strpos($classname, ‘CI_’) !== 0)
    • check if there a file starts with CI_ prefix
  • $file = APPPATH . ‘libraries/’ . $classname . ‘.php’;
    • get the complete location of the file and store it in the $file variable
  • if (file_exists($file) && is_file($file))
    • if the file exists and it is a valid file
  • @include_once($file);
    • include that file at once upon loading the page that is being accessed to

Let’s save the file and then refresh again the welcome page from our browser to check if it has resolved the issue:

Setting Up Base Controller 15

And yes it did! 🙂

To double check if it is really the Frontend Controller, let’s add the following script in the Frontend controller file:

var_dump(‘Welcome from Frontend_Controller!’);

Setting Up Base Controller 16.PNG

And then let’s refresh the welcome page again to see if it works:

Setting Up Base Controller 17.PNG

And sure it did. 🙂

So we’re done with our task for today…

To summarize, we’ve created three base controllers namely:

  • MY_Controller – that runs throughout our entire application no matter where we are
  • Frontend_Controller – that we’ll use to extend all frontend pages from
  • Admin_Controller – that we’ll use to extend all our admin pages from

So that’s it for today.

Next: Setting Up the Database using Migrations

 

 

Advertisements
 
 

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: