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.
#2

[eluser]ejangi[/eluser]
Nice work! I haven't had a need for it on any project so far, but I was wondering (in the back of my mind) how I might achieve this if I needed to.
#3

[eluser]CI Lee[/eluser]
This is great and is an good example of a use for hooks.

Can anyone think of a good way to allow certain controller to still run while the status is set to closed?

Lets say you wanted to have the admin controller still reachable while you entered in content or updated prices etc...

maybe a public / private set of flags somewhere?

-Lee
#4

[eluser]Rick Jolly[/eluser]
Personally, I'd just replace the index.php bootstrap file if I wanted to shutdown the site for maintenance. But using your code, I'd place a OPEN/CLOSED value in config.php since that is always loaded anyway. That way there isn't a database hit for the status on every request.
#5

[eluser]John Fuller[/eluser]
Yeah, I would probably screw something up in the database and accidentally route people to my naughty pics collection. I would just rename index.php and place an html file there to save myself the embarrassment.
#6

[eluser]Rick Jolly[/eluser]
[quote author="CI Lee" date="1199961095"]
Can anyone think of a good way to allow certain controller to still run while the status is set to closed?

Lets say you wanted to have the admin controller still reachable while you entered in content or updated prices etc...

maybe a public / private set of flags somewhere?

-Lee[/quote]
Could be something as simple as a redirect in the constructors of parent controllers. The parent controllers could have checks against values in config.php:
Code:
$config['accessAll'] = true;
$config['accessPublic'] = false;
$config['accessAdmin'] = true;
#7

[eluser]John Fuller[/eluser]
Tell you what though, that is much prettier than my first CI page. My first CI code was ugly. Think about CI Lee, but shaved. Ugly!
#8

[eluser]purpleparasol[/eluser]
I actually don't have stash of naughty pics :-S, so I think it's safe to keep the value in the database, but cache it, as suggested and use a special admin function to set/unset it from source when necessary.

Also incorporate CI Lee's idea of having some admin views/functions ignore it. The exact mechanics are not important to me right now, it was more that I could get anything beyond 'Hello World' to work.

I think if you were brought up on OO and MVC you probably don't realise how alien CI looks to someone who has coded procedurally for hundreds of years. Or so it feels. Thanks very much for all your feedback.
#9

[eluser]johnwbaxter[/eluser]
essexgirl, are you actually in essex?
#10

[eluser]purpleparasol[/eluser]
Sadly, no.




Theme © iAndrew 2016 - Forum software by © MyBB