• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] Loading model from hook - Fatal error: Call to a member function _assign_libraries() on a non-object...

#11
[eluser]Mirage[/eluser]
The problem you're running into is simply a [conceivable] limitation of a pre-fab framework like CI. In a glue type framework like Zend you'd automatically 'include' your libraries to get access to the functionality. With hooks, especially the ones that execute very early in the system, you have to let go of the Loader type calls because much of what's it depends on may not have been loaded yet.

Only count on what's available at a given point. Beyond that, there's nothing wrong with using php 'includes' to load your [CI] resources and instantiate them yourself.

That said - for Auth/ACL specific stuff I've created a _PrivilegedController.php which does what needs doing in it's constructor. I subclass my controllers from there and presto! Instant soup! No need to hack CI for this one.

It would be nice if in some subsequent release of CI the core would actually be re-written so that you can extend the CI controller the same way that you extend other libraries. The current desgin doesn't allow for this, but it's definitely possible.

Cheerio!

#12
[eluser]differ[/eluser]
Quote:That said - for Auth/ACL specific stuff I’ve created a _PrivilegedController.php which does what needs doing in it’s constructor. I subclass my controllers from there and presto! Instant soup! No need to hack CI for this one.

Can you tell more about that sentence above? Example code will be appreciated because I do not understand at all what you have written. How can I subclass my controller?

Thanks.

#13
[eluser]Mirage[/eluser]
privilegedController.php:

Code:
class PrivilegedController extends Controller {

        function PrivilegedController () {

            parent::Controller();

        }


        function login() {
            // reusable login code
            // override in your controller if you must
        }


        function logout() {
            // default logout function
            // override in your controller if you want

        }


        function _requireIdentity($loginURL=null) {
            $this->_checkAuth();

            // check session for credentials and redirect if not authenticated
            // e.g.
            if (!$this->session->user_data('logged_in') {
                header('Location: ' . (is_null($loginURL)?$this->config->item('defaultLoginURL'):$loginURL;
                exit;
            }
        }


        function _checkAuth() {
            // load needed plugins, libraries, whatever to do you auth thing
            // build up a user object with ACL credentials, and so forth
            // persist userID in the session
            // return true or false depending
        }


}


someController.php:

Code:
require_one(APPPATH.'controllers/privilegedController.php');

class SomeController extends PrivilegedController {

        function SomeController () {

            parent::PrivilegedController();
            // to secure the whole controller uncomment below
            // $this->_requireIdentity();

        }

        // public function
        function index() {
                $this->load->view('someindex.php');
        }

        // protected function
        function funcA() {
                $this->_requireIdentity();
                // the code below won't run if not logged in
                $this->load->view('funcaview.php');
        }


        // protected function with custom login page
        function funcB() {
                $this->_requireIdentity('/custom/loginpage');
                // the code below won't run if not logged in
                $this->load->view('funcaview.php');
        }

}


To expand this to support roles, etc should be pretty self explanatory.

HTH

#14
[eluser]differ[/eluser]
Thank you for great explanation. But look, in your example user have to replace old definition parent::Controller with new parent:TonguerivilegedController() where in my modification I will change code only once, not for each controller. My method is less invasive.

Thank guys for help.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.