
A framework can help us to define a set of rules/protocols for our application. However, application source codes can still be messed up if some general best practices aren’t followed well. Today, I will describe some Codeigniter best practices that will help you better organize source code and have less code written to do more. These will include using a reliable application structure and extending this framework using other third-party libraries to make it suitable for much bigger applications. Some may be general and can be used in any other application framework.
On the other hand, some will be Codeigniter framework-specific. It’s not a beginner’s tutorial to introduce the Codeigniter framework, so you must already know it. If you are a beginner, I strongly recommend that you better start with the Codeigniter application development tutorial first.
File/Directory structure:
You must take care of the source code files properly and organize them. In particular, if 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 to partial structure), and later, other developers and even yourself may get disappointed with that. So it’s better to keep them organized. If your application can be divided into several modules, do that and keep them in different directories inside the ‘controller’ directory. It will help you deal with many files in a single directory.
A better option is to use the HMVC extension. It is especially recommended that third-party plugins be integrated into your application easily and quickly.
On the image, you can see the directory structure of the ‘views’ section(the templates are smarty .tpl files, for your information). There, we have a shared directory where we can put the standard 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 return views of different pages, templates will also be different; thus, this standard originated. I have borrowed this idea from my learning experience in Asp.NET MVC, which supports the scaffolding of controllers in an automated way and generates a structure like this. I treat this as one of the most essential codeigniter best practices, as these are the most common places where file structures can be a real mess if not taken care of wisely.
Read The Complete CodeIgniter Tutorials Series By CodeSamplez.com
CodeIgniter Best Practice For Extending Core Classes:
Extending the core Codeigniter classes and libraries is always better to meet your application needs. Your application may have some generalized needs you must initiate on each controller. You can eliminate this by creating a custom ‘My_Controller’ class, which will include 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, I know that CodeIgniter’s view is also very rich. Still, for the huge advantage of template engines (you can search over the 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, you can consider reading my earlier article on getting started with Smarty. Also, if you have some basic ideas but want to integrate them into Codeigniter, you can read another article on integrating Smarty into Codeigniter.
Using Third Party Data Access Layer/ORM library:
Codeigniter has a perfect data manipulation library, an ‘active record class.’ Though there is excellent support from Codeigniter, developers are still interested in working, especially since some may prefer to work with ORM, but 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 very good documentation if you want to start with the doctrine itself.
Including JavaScript/CSS Libraries:
If you are developing a UI-rich application, you might also want to include some functional JavaScript and CSS libraries/frameworks. If you use a lot of JavaScript in the application, then using a JavaScript framework would be a good idea. For this, JavascriptMVC is my first choice. On the other hand, for a simple application without any framework, a JavaScript library like jQuery will help you make your task easier.
The same terms apply to CSS. Nowadays, several CSS frameworks will help you easily make your webpage cross-browser compatible. From then on, the 960 grid system, blueprint CSS, etc., can be one of your best choices. I started using Blueprints first and liked it. Recently, I became 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 integrated support for caching, sometimes, when accessing third-party API or similar cases, you will still have to rely on your custom caching implementation. As I mentioned while discussing the Codeigniter 2 features overview, it started supporting caching, so now you can easily have it in your application. You can read more about Codeigniter caching as further reference.
Final Words:
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, you can add/remove third-party library usage depending on your application type. 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 suggestions in mind. Happy coding 🙂
Discover more from CODESAMPLEZ.COM
Subscribe to get the latest posts sent to your email.
Excellent tips. Thanks.
definitely helpful for someone like me just getting started using codeigniter
Extremely useful your article, thanks a lot.
A lot of thanks