Welcome Guest, Not a member yet? Register   Sign In
My First CI Page - Hooks to Run Common Code
#1

[eluser]purpleparasol[/eluser]
Hi
I just started to use CI a few days ago, and as I am new to it, MVC and OO programming, I found it a bt difficult to start. However I've got the one thing working that I set out to do so please take a look and tell me if I am on the right track.

I want my eventual web application to have a status held in a database table. Currently that status is pretty simple - it is either OPEN or CLOSED. The idea being that if I should ever want to close the whole application down for maintenance or whatever, I can update the status field in the database to CLOSED, and no matter which page a person tries to browse to, they will get the "Application is closed for maintenance, back up in x minutes" page. Then as soon as I set the status back to OPEN, everything is available again.

I didn't want to repeat the code to do this in every controller so did this instead:

1)
Create a database table called application and a field in that table called status. Make it an enum field with the possible values OPEN or CLOSED.

2)
Write a controller and a view (for say, the home page) for the application and call it home.php. Write an extra view called closed.php containing the closed message.

3)
In the config/hooks.php file place the following code :

Code:
$hook['display_override'] = array(
'class'    => 'MY_Systemcheck',
'function' => 'checkstatus',
'filename' => 'MY_Systemcheck.php',
'filepath' => 'hooks'
);

4)
In application/hooks/MY_Systemcheck.php place the following code

Code:
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Systemcheck{
    var $status;
    var $CI;
    function MY_Systemcheck(){
        $this->CI =& get_instance();
        $query = $this->CI->db->get('application');
        if ($query->num_rows() > 0){
            $row = $query->row();
            $this->status = $row->status;
        }
    }

    function checkstatus(){
        $open_output = $this->CI->output->get_output();
        if ($this->status == "CLOSED"){
          $closed_view = "system/application/views/closed.php";
          $closed_output = $this->CI->load->file($closed_view, true);
          echo($closed_output);
        }
        else
          echo($open_output);
    }
}

5) Run home.php and if the application status in the database is OPEN, the home page will display. If the application status in the database is CLOSED, the special closed page, closed.php will display.

A hook was written to run an replace the naturally occurring display with the closed display in the case where the database says the application should be in a closed state.

It also demonstrates a way to run a common piece of code before every controller. You would just change the action site of the hook to pre_controller or similar.


Messages In This Thread
My First CI Page - Hooks to Run Common Code - by El Forum - 01-09-2008, 08:51 PM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-09-2008, 09:09 PM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-09-2008, 10:31 PM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-09-2008, 10:32 PM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-09-2008, 10:37 PM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-09-2008, 10:40 PM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-09-2008, 11:00 PM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 04:40 AM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 05:07 AM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 05:09 AM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 05:18 AM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 05:59 AM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 06:03 AM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 06:15 AM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 06:28 AM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 07:24 AM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 12:15 PM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 01:34 PM
My First CI Page - Hooks to Run Common Code - by El Forum - 01-10-2008, 02:25 PM



Theme © iAndrew 2016 - Forum software by © MyBB