views:

146

answers:

3

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?

A: 

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

Gordon
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.
kemp
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.
Gordon
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.
kemp
A: 

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

eCaroth
Nope, that's not how it works.
kemp
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....
eCaroth
+1  A: 
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');
ascotan
`$this->uri->segment()` is as good as it gets
kemp