Building Applications using CodeIgniter (Part 1) – File Structure
Creating web applications with CodeIgniter (CI) is quick and easy because CI handles a lot of the typical application requirements right out of the box (like session management, database abstraction and file uploading). I’ve developed a number of applications with CI now, including BadgeTracker and Sign-Up-Sheet.com, and while CI does handle the repetitive stuff it’s still up to you to create a scalable and easy to update application. Over the last couple of years I’ve come up with a pretty solid structure and set of files that I use whenever I’m building a new app and in this series I’m going to show you what I do so you can get ideas for your own apps.
In this first part of the series I’m going to talk about file structure. The first step to building your app is to get your folder and file structure setup so you have an idea where everything is going to go. If you wait to figure this out as you go along you run the risk of inconsistencies in your code so it’s good practice to just settle on a structure first thing.
CodeIgniter Default
By default, CI (version 1.7.1) presents you with the following structure:
- system
— application
—— application folders…
— cache
— codeigniter
— database
— fonts
— helpers
— language
— libraries
— logs
— plugins
— scaffolding
Everything lives inside the system folder. This isn’t necessarily a bad thing, but every time you want to get to your application to add or update files it’s just a hassle to go into system to get to your application. So, it’s pretty common practice to move your application folder outside of the system folder so it’s front and center in the document root (or whatever folder your developing in).
To accomplish this you will need to update the main index.php file outside of the system folder. Line 43 needs to change to “../application” instead of “application”. Once that update is done then CI shouldn’t be able to tell the difference.
Application Assets
It might be because I’m a neat freak or maybe it’s because of the brief exposure I had with Ruby on Rails (RoR) but I don’t like having my application root littered with folders for images and JavaScript files and such. I like a nice clean document root. So I create a folder at the root of my application called assets. Inside the assets folder I create 3 other directories for images, JavaScript and CSS files so it looks something like this:
- assets
— images
— js
— css
Now all of my non-php files are nicely organized in a single directory at the top of my application. From time to time I’ll also add a directory for videos but you get the idea.
Template Files
Another one of those common application tasks that CI handles, like I mentioned earlier, is templating. The views folder (inside application) makes up the V in MVC and that’s where all your design/display files should go. It’s taken me some time but I’ve settled on a pretty standard way I handle my application templating and it all revolves around a folder I create within views called _templates.
Instead of loading a bunch of view files one after another to make up my page, I prefer to create layout files which are complete web pages just with some variables acting as place folders for content to be inserted later. When it’s time to display a page I load a couple of partial views into variables (by returning the views as strings instead of displaying them) and then passing those variables to my master view file for display. The main benefit of this type of templating structure is that your whole application can use a just a hand full of layout files to handle page structure.
Within the _templates folder I create another folder called layouts. This is where the actual layout files live. Other include/template files are placed inside the _templates folder that are included in more than one layout file like navigation elements and such. I will also typically create other folders within _templates for things like system e-mails and the like.
Final Structure
That just about does it for our structure. I’ve listed the new structure below with just the new directories we should be left with and where they live.
- application
— views
—— _templates
——— layouts
- assets
— css
— images
— js
- system
At this point I typically move the files to the development environment and make sure the default CI welcome message is displayed. If your following along at home and don’t see the welcome screen you more than likely didn’t make the change to the index.php file I mentioned above when we moved the application folder outside system. Double check that and if all was done correctly you should be all set. If you continue to have problems, leave a comment and I’ll see what I can do to help.
Next up: Configuration
In part two of this series I’ll talk about configuring CI to load the common libraries we’ll need and creating a custom config file to keep information specific to our application.
Related posts:
- Building Applications using CodeIgniter (Part 2) – Configuration
- Building Applications using CodeIgniter (Part 4) – Code Templates
- Building Applications using CodeIgniter (Part 3) – Helpers
- Generating PDF files using CodeIgniter
- 10 Reasons Why CodeIgniter Rocks
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.


Great, I love CI, so it will be nice to learn something new from a PRO.
Keep it up. Good Job.
hey,Chris
really great articles. I came here form CI.Recently I am preparing one project for my company with CI and I will be eager to see your following “Building appliacation useing CI ” series.
From P.R.China Kevin
best wishes.
Thank you, nice walk-through! I’m gonna follow the series for sure.
Thanks again!
Interesting article but I’m confused by how you set up your templates. What do your layout files contain and I didn’t understand how your partial views work and how you send views as strings?
Regards
Our Typical project usually include websites in two languages, English and Arabic.
We usually have en & ar at the root folder, What do you advise for a folder structure in this kind of scenario.
Best,
Saad
Sorry for the late response to these comments!
Stephen, instead of splitting my templates into several files like header, footer, navigation, etc. I place all of the elements in one single layout file. The file then has variables for whatever content areas that change from page to page. I can then set those variables in an array and pass them to the template using CIs template library.
As far as loading templates as strings, you simply pass a 3rd true/false option to the $this->load->view() method. If true, the template will be returned as a string instead of being loaded and sent to the browser window. This is set as false by default. You can learn more by visiting http://codeigniter.com/user_guide/general/views.html (at the bottom).
Saad… you might have me stumped here. I’ve never had to deal with a multi-lingual CI project before. I think your on the right track though. If you create 2 folders in the root of views called en and ar and then mirror the folder structure between the two, so the only difference is the text on the pages, you should be able to store a language preference in a session or something and then load the appropriate views based on that preference.
Hope that helps.
Cool stuff. I love CI and will be using it a lot more so thanks for the info!
this is really a great article. However , I was wondering how do you organize file structure for front and backend, would you create admin folder?
Yes, I will typically create an “admin” or “backend” folder in the controllers directory. That way all admin controllers are separated from the rest of the project and they can only be accessed by /admin/controller. Beyond that though I really don’t do much in the way of separation. I will sometimes also organize assets into front and back end but not too often.
Hi Chris
I’m starting my move to CI and MVC approach, and I find your articles so interesting…
I’m still heaving troubles reproducing your views _template-layout engine method.
Could you kindly rebuild your CI install from this series of articles, by adding a view using a template and calling parcial views from the layout file?
Those 2 extra files (template+layout) would clear the fog…
Thanks, best wishes
Nuno, the download I created is really to be used as a template so I don’t necessarily want to add any extra files. Including a template within a layout is just as easy as loading another view file but within a view file. So, within your layout view file you simple say $this->load->view(‘_templates/footer’); or something like that. In this case I’m loading the footer.php view file within the _templates folder. That will display that template within the view.
As far as my layouts are concerned, I simply echo a bunch of variables within the view and then pass those parameters to the view from the controller. In some instances I’m returning views as strings to simplify things. To return a view as a string you simply add a TRUE parameter to the load function like so: $this->load->view(‘_templates/footer’, $params, TRUE); and that returns the specified view as a string into a variable which is then passed to the layout view.
Hope that helps.