In the previous tutorial, you learn how CodeIgniter is downloaded, installed, and viewed the welcome screen to make sure that it worked.
In this tutorial. you understand how the the purpose and working of \application folder in your CodeIgniter project. This tutorial is meant to be simple to understand so that new users of CodeIgniter get a general idea of how to organize the project in CodeIgniter.
You can skip this tutorial if you already understand the structure of \application\ folder’s contents.
First of all, go to the CodeIgniter folder and open the application folder as shown below.
Inside the \application\ folder, you will see the following directory structure.
Now I will try to make it simple for you to understand the purpose of these folders. Chose the folder name you are interested in learning about from this list below. Enjoy!
Generally, cache stores data that is to be used in future. So, lets say if your application uses data that is used a 100 times in a minute, it would be preferable to “cache your data” so that time and processing power is saved by reducing the “loading” process.
In CodeIgniter, you can perform Caching too. Once your page is loaded, subsequent loads will trigger your application to load from the cache. This will improve your web application’s performance.
In order to perform caching, all you need to do add the following code (replace n with value) in the controller on which you wish to perform caching.
Where, n is the number of minutes that you wish your page/controller to be cached. So if n is set 100, the controller will remain cached for 100 minutes. Afterwards, the page will be cached again.
In order to stop caching, simply remove the above code and caching will stop and pages will be loaded every time.
The config folder contains configuration files that allow you to configure your CodeIgniter application. For example, the “database.php” configuration file allows you to setup one or more databases to be used in your application. The “autoload.php” config file will load up libraries, helpers or even your custom defined config files so you don’t have to call them again and again in your project. This feature should be used on a need-basis.
Speaking of custom config files, it is also possible to define your own config files in this folder and call them up in your controller. This will allow you to make your web application more flexible. Many 3rd party tools would require you to put their config files in this folder.
Please note that you might put critical information in this folder so it is important to have security as your top priority when hosting your projects.
The flow of the web application is controlled by the “controller”. If the user presses a button, the controller handles its server side functionalities. If database request is made, it is moderated by the controller. If a web page is to be loaded, (i.e. a view) the controller does it. Roughly, we can say that controller is the brains of the CodeIgniter web application. If the controller does not work, nothing else associalted with it will!
The controller can pass parameters to the view, such as a username of a logged in user is retrieved from the database table of users. First the controller will use a model to retrieve username on the basis of logged in user’s authentication and identification. Afterwards, the controller will pass that username parameter to the view and view will be able to use it to show it on the browser.
As of CodeIgniter 3.0, the name of the Controller class file must start with an uppercase letter. So for instance, I would name my class as Calculator.php. The name “
calculator.php” is incorrect. My next tutorials will cover Controllers.
CodeIgniter has core classes in \system\ folder (outside the application folder). These core classes make up the CodeIgniter framework of PHP. In most cases, you won’t need to make any changes to the core classes of CodeIgniter.
But, if you actually need to modify the core classes of CodeIgniter, all you need to do is to create a class in “\application\core\” folder that has the same name as the core class file name in “\system\” folder. CodeIgniter will use your defined class instead of the native CodeIgniter class.
Again, it is not something that one does every day. But it is good to know that a wonderful feature such as this one does exist.
Need help in doing a task? Use a Helper!
A helper either a native one, or a custom defined helps you in tasks. Take a few examples:
- FORM HELPER: Used to create a form that works perfectly with CodeIgniter and hosts a lot of added features.Call it using the following line in Controller.
- DATE HELPER: This won’t get you on a date, but you will definitely get date features in your web applications. Date, time, time zones, it has so much to offer.
I will cover more on the helpers more in the coming tutorials.
While “\core\” folder hosts class files that completely replace native CodeIgniter files, the Hooks feature allows you to hook into the flow of CodeIgniter application and meddle with it. Here are some simpler features for you to know about what hooks can do:
- You can use hooks to performs actions before even system files are loaded.
- You can do something before the first controller is called (check routes.php in \application\config\ folder).
- You can perform an action between calling a controller’s constructor and calling its methods. Twisted, eh?
- Do something after a controller is completed its functions.
There are more features for you to learn and utilize if you are interested, check out the official documentation Here.
This folder allows you to create and organize text in a specific language. You can create files and folders for your desired language into this folder and use them in your project. All you need to do use the language helper and libraries accordingly.
The \application\library\ folder hosts your custom libraries. Lets say you are tired of implementing the “date” features over and over in your projects. Reinventing the wheel in other words. All you need to do is simply create a library and simply carry it throughout the projects.
The libraries can call models too. Roughly, libraries are similar to controllers.
Your application can be configured to store status messages such as errors, exception handling messages or your custom log messages. These get stored in this folder. This is a very helpful feature. If you have an error that is hard to understand, take a look at the logs here in this folder. The logs need to be turned on by modifying the “www.yourapp.com\index.php” file. Note that there are different levels of logging available for you to set.
Simply put, “Models” allow you to perform database queries. The controller requests the model to perform database queries, upon completion, the model (if programmed) will send the response data back to the controller and the controller may use it however we desire it.
Many times, if you download some third party plugin for usage with CodeIgniter, its core files will be put in this folder. It is well known that CodeIgniter is flexible enough for almost every 3rd party plugin to be set up in its environment without a hassle.
All your web design elements come here. You can keep the assets out of here, but the html code goes in here as “.php” files. The controller can call views. For example, a chat bubble can be made using a view and called on demand by the controller to be shown/overlaid in a different view. Moreover, It is possible to create a control panel separately and its sub pages separately in different views. This helps us in minimizing code drastically, thanks to CodeIgniter.
In the next tutorials, I will get started with teaching you how to program in CodeIgniter 3. Let me know in comments if you need a better explanation of a folder. Don’t forget to share :D. Thanks for reading. I hope it helped.