CodeIgniter Forums
Application structure? - 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: Application structure? (/thread-29687.html)



Application structure? - El Forum - 04-18-2010

[eluser]Rein[/eluser]
Dear CI-users,

I'm pretty new to CodeIgniter and I'm very excited. From what I see from this framework, this is exactly what I'm looking for.

Now I have a question regarding the structure of my application.

I'm trying to get my application to automaticaly show the controllers available. I created a model to do this. But I think this is a wrong approach. Here is my "model":

Code:
<?
class Modules extends Model {

    function Modules()
    {
        parent::Model();

    }
    
    function getmodules()
    {
        $handle = opendir('./system/application/controllers');
        while(false != ($file = readdir($handle))){
            $extension = strtolower(substr(strrchr($file, '.'), 1));
            if($extension == 'php'){
                $file = str_replace('.php', '', $file);
                $modules[] = $file;
            }
        }
        return $modules;
    }
    
}
?>

It just feels strange to load this model directly in my layout view file to have a menu showing the controllers.


What would you guys do to get the desired result?


Thanks for any help!


An exciting CI-starter.


Application structure? - El Forum - 04-18-2010

[eluser]umefarooq[/eluser]
in MVC model can not call controller, you are breaking MVC rules well if you want to create modular layout or and want to create widget for you layout try this plugin will solve you problem

http://ellislab.com/forums/viewthread/109584/P40/


Application structure? - El Forum - 04-18-2010

[eluser]cahva[/eluser]
Well hes not actually using the controllers in model, just reading the filelist from controllers dir. So I dont think hes doing anything bad here. As I recall, models can be used for fileaccess also besides database.

But one thing is for sure, you should never call model inside the view! Call that model from controller and pass it to the view.

If you need that modules list in every controller(to be used in your layout), use MY_Controller technique to set it up so you wont have to call that model in every controller. Just search for MY_Controller and you should see how you can do that.


Application structure? - El Forum - 04-18-2010

[eluser]Rein[/eluser]
Thanks for the replies.

I tried searching for MY_Controller and found out I can run code for all of my controllers. I came up with the following code;

Code:
<?php
class MY_Controller extends Controller{
        
    function MY_Controller()
    {
        parent::Controller();
        echo 'test';        
        $this->load->model('Modules');
        $data['modulelist'] = $this->Modules->getmodules();
    }

}
?>

Every controller from within my controller-folder now calls the echo, but the $modulelist is not available for use in my views. What's wrong here?

Thank you!


Application structure? - El Forum - 04-18-2010

[eluser]Jelmer[/eluser]
That's because the $data array is only available within the constructor and not outside it.

You could save it as a object property and then you'll be able to access it outside the constructor. Like this:
Code:
<?php
class MY_Controller extends Controller {

    var $modulelist;

    function MY_Controller()
    {
        parent::Controller();
        $this->load->model('Modules');
        $this->modulelist = $this->Modules->getmodules();
    }

}

And then in your controller you can access it as $this->modulelist, just be carefull not to use a variable name that could conflict with a library or model because it shares the same name (it will be overwritten the second you load the library).


Application structure? - El Forum - 04-18-2010

[eluser]cahva[/eluser]
Well there are 2 options:

1. Save the modulelist in $this->data['modulelist'] and then you can access that and pass $this->data to your layout.
2. Use $this->load->vars($data) after you are done with it in MY_Controller. This will make $modulelist accessible from everywhere including views.

So remember, if you set a variable using $data = 'something', its only accessible on that methods scope. So when you set $this->data in MY_Controller and your controllers extend from it, they will be available.


Application structure? - El Forum - 04-18-2010

[eluser]Rein[/eluser]
Works like a charm, thanks for the fast reply. Planning on playing around with it a bit more!