CodeIgniter Forums
post_controller_constructor hook not loading $this->session - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: post_controller_constructor hook not loading $this->session (/thread-39553.html)



post_controller_constructor hook not loading $this->session - El Forum - 03-14-2011

[eluser]Webnet[/eluser]
My problem is that I can't access $this->session within my Application controller. Is this functionality not available to the post_controller_constructor hook ?

hooks.php

Code:
$hook['post_controller_constructor'] = array(
    array(
        'class' => 'application',
        'function' => 'preController',
        'filename' => 'application.php',
        'filepath' => 'controllers'
    )
);

application.php

Code:
class Application extends Controller {
    /*
     * The application controls logic which is global to the entire application and not on any specific page.
     */
    public function __construct() {
        parent::Controller();
        
        $this->load->library('session');
        print_r($this->session);
        exit;
    }
}



post_controller_constructor hook not loading $this->session - El Forum - 03-14-2011

[eluser]bubbafoley[/eluser]
If you're using CI 2.0 it should look like this:

Code:
class Application extends CI_Controller {
    /*
     * The application controls logic which is global to the entire application and not on any specific page.
     */
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        print_r($this->session);
        exit;
    }
}

What is that hook supposed to be doing? It's looking for a function preController() in application/controllers/application.php which doesn't appear to exist.


post_controller_constructor hook not loading $this->session - El Forum - 03-14-2011

[eluser]Webnet[/eluser]
The function does exist, I didn't post the whole class because it shouldn't matter, it fails at the constructor.


post_controller_constructor hook not loading $this->session - El Forum - 03-14-2011

[eluser]bubbafoley[/eluser]
what is the error you are getting?


post_controller_constructor hook not loading $this->session - El Forum - 03-14-2011

[eluser]Webnet[/eluser]
The below class works, but my original Application controller that's loaded by the hook does not load the session.

Code:
class AbstractController extends Controller {
    protected $current_user,
              $current_company,
              $jsFiles;

    /**
     * AbstractController Construct
     *
     * @return AbstractController
     */
    public function __construct($skipRedirect = false) {
        parent::Controller();
        
        $this->load->library('session');
        $this->load->library('form_validation');
        $this->load->database();
    }

Fatal error: Call to a member function userdata() on a non-object in application.php on line 47


post_controller_constructor hook not loading $this->session - El Forum - 03-14-2011

[eluser]bubbafoley[/eluser]
your hook class should not be a controller.

try this:

hooks.php
Code:
$hook['post_controller_constructor'] = array(
    array(
        'class' => 'Application',
        'function' => 'preController',
        'filename' => 'Application.php',
        'filepath' => 'hooks'
    )
);

application/hooks/application.php
Code:
<?php

class Application {

  function preController()
  {
      $ci =& get_instance();
      print_r($ci->session);
  }

}



post_controller_constructor hook not loading $this->session - El Forum - 03-14-2011

[eluser]Webnet[/eluser]
Thanks!