Welcome Guest, Not a member yet? Register   Sign In
Setting final output
#1

[eluser]Madmartigan1[/eluser]
In my admin panel, there is a setting for "maintenance mode", which if enabled will "shut down" the front end site by redirecting to the base url and displaying a message. This is done via MY_Controller. Something like this:

Code:
// "Public_Controller" controls all front-end pages

class Public_Controller extends MY_Controller {

    public function __construct()
    {
        parent::__construct();

        if (config_item('maintenance_mode') === TRUE)
        {
            $this->output->set_output("The site is down for a bit.");

            // at this point, we want to stop execution

        }
    }
}

The problem:
The maintenance message displays as expected, but I cannot stop the rest of the controller from running - so, along with the maintenance message, the rest of the view loads as normal below it. If I call exit() or die() in the Public_Contoller construct, nothing will happen at all.

I was surprised that set_output() behaves this way. I thought it would send the final output and stop processing. I should note that I am using CI 2.0.

I'm open to another way of handling "maintenance_mode" if this can't work.

Any suggestions or insight?
#2

[eluser]smilie[/eluser]
Not tested, but could you try:

die($this->output->set_output("The site is down for a bit."));

Not sure it will work tho' Smile

Cheers,
Smilie
#3

[eluser]pickupman[/eluser]
Check out [url="http://ellislab.com/codeigniter/user-guide/general/hooks.html"]hooks[/url] in the user guide. You can use a hook on "display_override" to control the final output.
#4

[eluser]Madmartigan1[/eluser]
@smilie - If I call exit() or die() in the Public_Contoller construct, nothing will happen at all, as I am actually loading a view - not just printing the message. I believe it never gets to the final output stage.

@pickupman - I considered a hook, but decided it was too much trouble as I need all of the assets available to Public_Controller in order to run the code (for instance, looking up the maintenance message and if the user is an admin or not, along with loading all the usual). Maybe I need to take another look at the display_override hook point however.

What about _remap()? Could that help?
#5

[eluser]pickupman[/eluser]
_remap() would work as well, or if you don't need anything fancy just use:
Code:
show_error('Website down for maintenance');
#6

[eluser]Madmartigan1[/eluser]
In reality, we're loading a normal template, just with the maintenance message replacing the actual content.
Some parts of the site still need to work as normal, it's not a "true" maintenance mode. We do this for sites before they go live so we can still work on them on a live server without hassle. (Admins don't get the warning message). It's also used to pull the plug on a deadbeat client Smile

I've never used _remap() in MY_Controller, I think I will try it.

The main thing I'm wondering about is that set_output() is not the solution that I thought it was. Either I'm using it wrong, or the docs are deceiving.
#7

[eluser]Madmartigan1[/eluser]
_remap() worked perfectly. Thanks for the help everyone.

Code:
// In Public_Controller...

    function _remap($method)
    {

            if ($this->settings->item('maintenance_mode') === '1' && ! $this->auth->is_admin())
            {
                
                // Redirect to base url
                if (current_url() !== base_url())
                {
                    redirect();
                }
                
                $view = $this->settings->item('maintenance_mode_message');

                $this->template
                    ->set_layout('one_column')
                    ->load(false, $view);
                    
                
            }
            else
            {
                $this->$method();
            }
    }
}
#8

[eluser]pickupman[/eluser]
Glad, you got it working, and more important for sharing your code with others.
#9

[eluser]John_Betong[/eluser]
I am not sure but it looks as though all your pages will be redirected which will have a negative performance effect.

Because the ./config/config.php first controller that is called according to the verbose error logging I would do this:
 
 
./config/config/php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
if(1)
{
    echo 'Site down for maintenance';die;
}
....

....

....
 
 
 
#10

[eluser]Madmartigan1[/eluser]
"Performance" during a period when the entire site will be down is probably not an issue, and besides - it only redirects, not much of a burden. This is a rare case scenario anyways.

Also, I need the site to be available in some controllers, so I can log in and reactivate it Smile
Doing this in the config file won't give access to the assets I need to check anything in the first place.

Are you saying that simply calling _remap() in every [front-end] controller (via MY_Controller) will affect performance in a negative way?

Correct me if I am wrong.




Theme © iAndrew 2016 - Forum software by © MyBB