Hi, as i said, I’m stating new project – CMS that developed in CodeIgniter. In this and next few posts I’ll describe the process of developing this app, the infrastructure I create for this app and share some idea with you.
If you haven’t read my Must Have Feature in your CMS post, please do so right now, I will include those features in my application.
My Folders Structure
When you develop an application that is pretty big, and in my idea its big when you have 2 or more modules [like "Photo Gallery" and "eShop"], you must separate the controllers-models-views in order to have understandable application structure.
I find it more useful to put all my controllers of the Photo Gallery module in folder named gallery inside my controllers folder – so my urls will look like: myapp.com/gallery/category/new -> when the gallery is my folder, category is my controller and new is my method inside the controller.
The same thing I’m doing with Views – and with views its highly important because you have a lot of them. So when you load some view in your controller, you use:
$this->load->view($this->moduleName.'/addNewForm', '', TRUE);
and of course the moduleName is a field that contains the folder name inside the views folder of your module’s views.
(For some reason, doing this with models isn’t working, so my models folder is pretty ugly in all applications I develop).
I decided to look for the problem with this folder structure in my models – I noticed that I didn’t loaded them correctly, now I see that this structure works for models too.
Template system
My template system is always pretty easy. I use the load->view functionality [for those who aren't familiar with CodeIgniter, load->view is basically loading HTML file and pass an array of data to it - the array extracted and data inserted in the right place in your HTML file] but differently then what is shown in CodeIgniter’s videos.
I create a library – class named TPL, this class loads my basic template of the application. The array contains my content in the content area, the content in the sidebar and etc.
Usually I use a clean design too [for LightWindow for example], or maybe there is certain areas where I don’t need to show the sidebar, or maybe I supposed only to show the content itself that passed to this class. So my class is strong enough to know which view it supposed to load each time -> maybe with uri segments? or maybe with params I send to the TPL class?
Each method in my controllers creates eventually the content by loading its own views, but I never load the main view from the controller directly! I use the TPL class for it. I hope you understand why – mainly because of code duplication, its not scalable, I have a lot of items in my design [tabs, menus, tree navigation...] and its really important to control all of these and doing it in one place.
Consider this simple idea for your applications, for now – it did everything I needed.
MY_Controller
CodeIgniter allows you to extend core libraries, and I use it to extend models and controllers. In the future posts I’ll explain deeply about what actually I do, but here I want you to get the idea.
Almost in each Controller in your application you have the same methods and basic functionality, you have an items list, you have the Add New Item form, the method that inserts the data in your database.. there is a chance this code might be duplicated, and I wish to prevent it, write good code
In my controller, I add these basic methods and create a basic infrastructure for them to work in each of my controllers. In my next post I’ll explain it.. Until then I wish to hear your opinion!
[...] Developing Content Managment System With CodeIgniter – Part 2 Hi, if you haven't read my previous post, please read it now: Developing Content Management System With CodeIgniter Part 1. [...]
Developing Content Managment System With CodeIgniter – Part 2 « Udi Mosayev
6 Jan 10 at 12:14
u rock man!
these tuts are grt.
Prabhjeet
8 Mar 10 at 09:55
well, I want to share one thing here dude!
I manage my Models in different directories.
Here is what I do:
for example if i have directory structure for my few models like this:
http://www.example.com/application/models/admin/madmins.php
http://www.example.com/application/models/admin/mpages.php
http://www.example.com/application/models/mwidgets.php
i put them in cofig file as below:
$autoload['model'] = array(array(‘admin/MAdmins’, ‘admin/MPages’), ‘MWidgets’);
and it manage my models also very clean.
lol, and sorry for my english, its my 2nd language.
Prabhjeet
8 Mar 10 at 10:06
Hi,
first of all – thanks for you comments, its always nice to get such good feedback from readers.
Now, the auto-load feature is great! and I do use it, but only for models I access from more then 1 place in my application.
Most of the time, the pages accessed from 1 module [3-4 controllers in my /pages/ module] and There is no need for me to auto-load -models/pages/mpage.php
To read more about the way I structure my applications you can read the next posts of this series.
Thank you!
Udi Mosayev
8 Mar 10 at 21:44