Passing parameters to controller's constructor
Asked Answered
L

3

6

I have a controller which has several methods which should all share common informations. Let's say my URI format is like this:

http://server/users/id/admin/index
http://server/users/id/admin/new
http://server/users/id/admin/list
http://server/users/id/admin/delete

I need to retrieve some informations from the database for id and have them available for all methods instead of writing a line in each of them to call the model. How can I do this?

Lapboard answered 18/12, 2009 at 12:55 Comment(0)
A
4
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');
Alten answered 18/12, 2009 at 22:45 Comment(0)
C
0

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 :(

Caridadcarie answered 18/12, 2009 at 13:19 Comment(3)
Well the problem is getting id into play: I know how to get it inside methods but not in the constructor. By the way, you can use PHP5 syntax too with codeigniter.Lapboard
If CI's methods for getting the id are not available in the constructor yet, you can still parse it from the Request-URI in the $_SERVER superglobal.Caridadcarie
Yes I guess I could do that, I was looking for a cleaner solution than manually parsing $_SERVER['REQUEST_URI']. After all that's what a framework should do.Lapboard
K
-2

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...

Kortneykoruna answered 18/12, 2009 at 15:22 Comment(1)
Sorry, unfamiliar with codeignitor, just assumed based on all the structuring that it would use mod_rewrite. But you know what they say about assuming....Kortneykoruna

© 2022 - 2024 — McMap. All rights reserved.