Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 85 revisions

This is a solution for people that are looking for a way to keep associated models, views and controllers in the same directory and thereby making their application more modular.

[h2]Installation[/h2] First of all you should go ahead and grab yourself a copy of the File:Modular_Separation.zip.

When you are done grabbing, you should place both files in your [b]application/libraries/[/b]-folder.

And that is it!

[h3]... Unless you are running PHP 4[/h3] In which case you will have to edit the file [i]Base4.php[/i] in your codeigniter-folder.

[h2]Usage[/h2] A module consists of at least one controller and might include a number of models and/or views. These must be placed in the [b]application/modules/MODULENAME/[/b] folder (with [b]MODULENAME[/b] being the name of your module, ie. blog, forum, etc).

The files must be placed in the following folders:

[b]/system/application/modules/MODULENAME/models/ /system/application/modules/MODULENAME/views/ /system/application/modules/MODULENAME/controllers/[/b]

Your controllers will then be accessible as [b]www.example.com/MODULENAME/CONTROLLER/METHOD[/b].

Consider this URI:

[b]www.example.com/blog/entries/view/[/b]

In the above example, CI will open the [b]application/modules/blog/controllers/entries.php[/b] controller and call the [b]view[/b] method within it.

However, an URI like this:

[b]www.example.com/blog/[/b]

Will only look for the controller with the same name as the module, ie. [b]application/modules/blog/controllers/blog.php[/b] controller and call the [b]index[/b] method.

In order to let you call methods from your default controller (the one with the same name as your module, application/modules/blog/controllers/blog.php), CI will in fact handle this URI:

[b]www.example.com/blog/entries/view/[/b]

By first looking for a method called entries in [b]application/modules/blog/controllers/blog.php[/b] and then for a [b]application/modules/blog/controllers/entries.php[/b] and call its [b]view[/b] method.

You can even put controllers in sub-folders. If you do, the same rules apply as above, except the URI will need to have the FOLDERNAME before your MODULENAME:

[b]www.example.com/FOLDERNAME/MODULENAME/METHOD/[/b] (if default controllers has the called METHOD)

or

[b]www.example.com/FOLDERNAME/MODULENAME/CONTROLLER/METHOD/[/b]

[h3]Important note for PHP4 users[/h3] Because of a bug, if you are using PHP4, CI does not extend the loader class. Until Rick fixes this, you can do it yourself by editing the [b]Base4.php[/b] file like this:

[b]system/codeigniter/Base4.php[/b] [code]... class CI_Base extends MY_Loader { ...[/code] That is, replacing CI_Loader with MY_Loader.

Clone this wiki locally