• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Development / Codeigniter Best Practices And Tips For Efficient Development

Codeigniter Best Practices And Tips For Efficient Development

November 16, 2011 by Rana Ahsan 5 Comments

codeigniter best practices

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.

File/Directory structure:

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.

Directory Structure For Codeigniter Views

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.

Read The Complete CodeIgniter Tutorials Series By CodeSamplez.com

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.

Including JavaScript/CSS Libraries:

If your developing a UI-Rich application, then you also might be interested to include some useful JavaScript and/or CSS libraries/frameworks. If you are going to use a lot of JavaScript in the application, then using a JavaScript framework will be a good idea. For this JavascriptMVC is my first choice. On the other hand, for simple application, without using any framework, using a JavaScript library like jQuery will help you a lot in making your task easier.

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.

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, 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 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Development Tagged With: codeigniter, doctrine, php, smarty, web application

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. Carlos Vergara says

    January 26, 2012 at 9:41 am

    Excellent tips. Thanks.

    Reply
  2. john says

    August 12, 2012 at 2:29 pm

    definitely helpful for someone like me just getting started using codeigniter

    Reply
  3. Ricardo Fabrizio says

    March 26, 2016 at 11:12 pm

    Extremely useful your article, thanks a lot.

    Reply
  4. Krishna says

    August 24, 2016 at 2:15 am

    A lot of thanks

    Reply

Trackbacks

  1. Codeigniter Bundle With Popular Libraries And Best Practices | codesamplez.com says:
    December 20, 2012 at 2:11 am

    […] practices: This project also focuses on maintaining the Codeigniter best practices , I mentioned before. So, hopefully, as you use this, you will understand in more depth, how its […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Get Facebook C# Api Access Token
    Get Facebook C# Api Access Token
  • Beginning Codeigniter Application Development
    Beginning Codeigniter Application Development
  • How To Use Hotkeys/Keyboard Events In WPF Application Using C#
    How To Use Hotkeys/Keyboard Events In WPF Application Using C#
  • Control HTML5 Audio With Jquery
    Control HTML5 Audio With Jquery

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023