Welcome Guest, Not a member yet? Register   Sign In
Problems with 'pre_controller' hook point
#1

[eluser]Marcus Cavalcanti[/eluser]
Hi Everybody,

In my application I have many controllers that extended others controllers, then to include these controllers I use require PHP function in each controller that I need to do this inclusion.

As the code below:

Code:
require_once 'BaseController.php';

class Home extends BaseController {
    
    function Home () {
        parent::BaseController();
    }

        function index () {
        }
}

Then I thought about doing this in a more encapsulated way using Hooks. I created a hook 'pre_controller' and in the class associated with this hook I have a code as below:

Code:
class ClassLoader {
    private $uri;
    
    function ClassLoader() {
        $this->uri =& load_class('URI');
    }
    
    function loadDependencies() {
        switch ( $this->uri->segment(1) ) {
            case 'booking':
               require_once APPPATH . 'controllers/search.php';
               break;
            default:
                require_once APPPATH . 'controllers/BaseController.php';
        }                
    }
}

The problem is when I run my application, CodeIgniter reports that the file was not included, where it is concluded that the controller is being called before my hook.

Code:
Fatal error: Class 'BaseController' not found in /home/marcus/public_html/bbbrasil/system/application/frontend/controllers/home.php on line 5

So my question is: with the 'pre_controller' my hook should not be called before any controller?
#2

[eluser]m4rw3r[/eluser]
Your case is unfortunate, because you need a hook here:
Code:
// from CodeIgniter.php
    require(BASEPATH.'codeigniter/Base5'.EXT);
}

// Load the base controller class
load_class('Controller', FALSE);

// HERE YOU WANT YOUR HOOK

// Load the local application controller
// Note: The Router class automatically validates the controller path.  If this include fails it
// means that the default controller in the Routes.php file is not resolving to something valid.
if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT))
{
    show_error('Unable to load your default controller.  Please make sure the controller specified in your Routes.php file is valid.');
}

include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT);
Also, you can use the pre_system or cache_override (return false) and use this code:
Code:
if (floor(phpversion()) < 5)
{
    load_class('Loader', FALSE);
    require(BASEPATH.'codeigniter/Base4'.EXT);
}
else
{
    require(BASEPATH.'codeigniter/Base5'.EXT);
}

// Load the base controller class
load_class('Controller', FALSE);
And then you can require your own special controller base classes.
#3

[eluser]Marcus Cavalcanti[/eluser]
For me it should be simple, if I have a hook of type 'pre_controller' all that was associated with this hook should be called before any application controller is loaded (not the CI base controller). But thanks for your answers.
#4

[eluser]m4rw3r[/eluser]
Yep, I think they missed the hook "post_load_class_controller".
#5

[eluser]Marcus Cavalcanti[/eluser]
Could be as a suggestion for a next version of CI Smile




Theme © iAndrew 2016 - Forum software by © MyBB