Welcome Guest, Not a member yet? Register   Sign In
Alternative Controller
#5

[eluser]tonanbarbarian[/eluser]
I think the reason that autoload is not working in this case is that autoload is done AFTER the controller is created.

The way I have done this before is to override the Router. The advantage of this method is that you can determine which extension library is needed based on the route

libraries/MY_Router.php
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Router extends CI_Router {
  
  /**
   * Constructor
   */
  function MY_Router() {
    parent::CI_Router();

    // at this point we know the current route
    // so determine what libraries we need to load
    if ($this->directory == 'admin/')
      // admin so load the backend
      load_class('Backend', false);
    else
      // front end
      load_class('Frontend', false);

  } // MY_Router()

} // class MY_Router
Since the Router knows where we need to go immediately after it is instanciated we can just check the directory and if it is 'admin/' or whatever else you want, we know to load the appropriate class.
We use load_class rather than $CI->load->library because at this point there is not controller instanciated.
The 2nd parameter of load_class being set to false will ensure that the class is loaded but not instanciated.


Messages In This Thread
Alternative Controller - by El Forum - 12-01-2007, 04:31 AM
Alternative Controller - by El Forum - 12-01-2007, 05:03 AM
Alternative Controller - by El Forum - 12-08-2007, 05:01 AM
Alternative Controller - by El Forum - 12-08-2007, 05:25 AM
Alternative Controller - by El Forum - 12-08-2007, 05:42 AM
Alternative Controller - by El Forum - 12-08-2007, 05:45 AM
Alternative Controller - by El Forum - 12-08-2007, 11:20 AM
Alternative Controller - by El Forum - 12-09-2007, 08:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB