A framework can help us to define a set of rules/protocols for our application. However, application source codes can be still messed up if some general best practices aren’t followed well. Today, I will try to describe some Codeigniter best practices which will help you for better organize of source code and have less code written to do more. These will include how to use a reliable application structure and how can you extend this framework by using other third-party libraries to make suitable for much bigger application. Some of them may be general and can be used any other application framework also. On the other hand, some will be Codeigniter framework specific. It’s not beginners tutorial to introduce Codeigniter framework, so its required that, you already know it . If you are a beginner, I will strongly recommend that you better start with beginning Codeigniter application development tutorial first.
It’s very important that, you must take care of the source code files properly and how they are organized. Specially, i you are working with a large-scale application, and don’t take care of the structure, it may get a little messy with large files(even if Codeigniter force you for partial structure) and later other developers and even yourself may get disappointed with hat. So, it’s better to keep them organized. If your application can be divided in several modules, do that and keep them in different directory inside the ‘controller’ directory. it will help you from dealing with a lot of files in a single directory.
A better option is to use HMVC extension. It specially is very much recommended, if you want to integrate third-party plugins into your application very easily and quickly.
On the image, you can see the directory structure of ‘views’ section(the templates are smarty .tpl files, for your information). There, we have a common directory where can put the common templates like header,footer etc. on the modules section, we will have a directory for each controller. As a controller has several methods and most of them returns views of different pages, templates will also be different and thus this standard originated from. I have borrowed this idea from my learning experience in Asp.NET MVC which does support scaffolding of controllers in automated way and generate structure like this. I treat this as one of the most important codeigniter best practices as this are most common places where file structures can be a real mess if not taken care of wisely.
CodeIgniter Best Practice For Extending Core classes:
It’s always better to extend the core Codeigniter classes and libraries to meet your application needs. Like your application may have some generalized needs which you might need to initiate on each controller. You can eliminate this by creating a custom ‘My_Controller’ class which will includes your settings and all other controllers on the application will inherit this new custom class. As a result, we will get these new custom functions on all controllers. Similarly, say you are using Codeigniter’s ‘form_validation’ class, but need to added some new validation rule like URL validation or phone number validation(which doesn’t exist on core Codeigniter framework), then its best to extend this library as ‘My_Form_Validation’.
Custom Template Engine For Rendering Views:
Though many developers choose to use codeigniter views and not any other third-party template engines and I know that, codeigniter’s view is also very rich. Still, for the huge advantage of template engines (you can search over internet), I like to use template engines and smarty is my best choice. It’s also easy to integrate on codeigniter. If you are very new to smarty, then you can consider reading my earlier article on getting started with smarty . Also, if you have some basic idea, but looking for integrating it on codeigniter, you can read another article on how to integrate smarty in codeigniter.
Using Third Party Data Access Layer/ORM library:
Codeigniter has a very good data manipulation library along with itself, which is ‘active record class’. Though it’s a very good support from Codeigniter, still developers interested to work specially, like some may want to prefer to work with ORM, it isn’t enough. For that, you may want to use third-party library, doctrine is my choice. You can read my earlier article on integrating doctrine with Codeigniter. Also, they maintain a very good documentation to start with, if you are interested to start with doctrine itself.
Same terms go to CSS. Now a days, there are a number of CSS framework that will help you make your webpage cross browser compatible very easily. From then, 960 grid system, blueprint CSS etc can be one of your best choice. I have started using blueprintcss first and liked to use it. Recently, I become interested in twitter bootstrap and will use it on my projects soon.
Implement Your Custom Caching If Needed:
Though Doctrine ORM and Smarty Template Engine have their own integrated support for caching, sometimes, when accessing third-party API or similar cases, you will still have to rely on your own custom caching implementation. As I mentioned while discussing about codeigniter 2 features overview that, it started supported caching, now you can have it easily in your application as well. You can read more about codeigniter caching as further reference.
Finally, I want to remind you that, it’s totally up to you how you want your application to be organized. I have tried the above techniques and it seemed helpful to me, that’s why I am sharing them with you so that you may get some help too. However, depending on your application type, you can decide adding/removing the third-party library usage. I have integrated these codeigniter best practices in my codeigniterplus project. You can check them out practically if interested. Let me know if you have any questions or any suggestions in mind. Happy coding 🙂