Passing parameters to controller's constructor Passing parameters to controller's constructor codeigniter codeigniter

Passing parameters to controller's constructor


class users extends Controller { private $mydata = array(); function users() {        parent::Controller();     ....     $this->mydata = $this->model->get_stuff($this->uri->segment(2)); } function index() {      $this->mydata; //hello data! }

Here I simply hardcoded the array (which probably is a really bad idea). Nevertheless you can store the data in a codeigniter session if you need to. Codeigniter can store this data in a cookie (if it's total is less than 4kb) otherwise you can store bigger blobs of data in the database (see the docs on how to do this).

See: http://codeigniter.com/user_guide/libraries/sessions.html

Subsection: Saving Session Data to a Database

Here's some session exercise:

$this->session->set_userdata('mydata', $mydata);....$mydata = $this->session->userdata('mydata');


If this cannot be solved from CodeIgniters Hook mechanism, you could override the constructor method in your controller and call your own. Judging from their SVN repository you'd probably would do something like

class YourController extends Controller{    function YourController()    {        parent::Controller();        $this->_preDispatch();    }    function _preDispatch()    {         // any code you want to run before the controller action is called    }

Might be that the call to preDispatch has to be before the call to parent. Just try it and see if it works. I didnt know they still use PHP4 syntax. Ugh :(


Based on your url structure, and the fact that codeignitor uses a MVC pattern, I'm assuming you're using mod_rewrite to format the url path into a query string for index.php. If this is the case, the value of "id" should be available at $_REQUEST['id'] at any point in the execution of the script...