I have this code in application\config\hooks.php
$hook['post_controller_constructor'] = array(
'class' => 'maintenance',
'function' => 'maintenance',
'filename' => 'maintenance.php',
'filepath' => 'hooks',
'params' => array()
);
and this code in application\hooks\maintenance.php
class maintenance
{
var $CI;
public function maintenance()
{
echo "Test";
}
}
and this code in application\config\config_maintenance.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['maintenance'] = true;
and here's how my controller looks like:
<?php
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model(array('home_model'));
}
function index()
{
$this->load->view('home');
}
}
When the code is run, "Test" is echoed twice on the page if I don't add "exit;" after the echo statement. Does that means that "post_controller_constructor" is being called twice?
I am wondering why is this because as per CI documentation
post_controller_constructor: Called immediately after your controller is instantiated, but prior to any method calls happening.
Okay, the problem lies in your Maintenance class and in your hook definition. You call the hook maintenance
and the function maintenance
. If you name a method the same way as the class, then this method is the class constructor. Go ahead and rename your method:
hooks.php
$hook['post_controller_constructor'] = array(
'class' => 'maintenance',
'function' => 'differentName',
'filename' => 'maintenance.php',
'filepath' => 'hooks',
'params' => array()
);
maintenance.php
class maintenance
{
var $CI;
public function differentName()
{
echo "Test";
}
}