Codeigniter - making my controllers more DRY Codeigniter - making my controllers more DRY codeigniter codeigniter

Codeigniter - making my controllers more DRY


You need to create base controllers that extend the CI_Controller. Then all your controllers extend a certain base controller you created, depending on what needs to be done in all cases that controller is called.

In application/core create a file called MY_controller.php (the prefix can be changed in config):

class MY_Controller extends CI_Controller {    function __construct()    {        parent::__construct();            /* Widgets are only prepared -- they will be fetched and rendered once layout->render is called.               This saves the overhead of reading the files on requests where layout isn't rendered.            */                          $this->layout->prepare_widget( "widgets/navigation", "navigation_widget" );        $this->layout->prepare_widget( "widgets/footer", "footer_widget"  );        $this->layout->prepare_widget( "widgets/twitter", "twitter_widget" );    }}class Public_Controller extends MY_Controller {    function __construct()    {        parent::__construct();    }}class Admin_Controller extends MY_Controller {    function __construct()    {        parent::__construct();            if( !$this->user->has_permissions( PERMISSION_ADMIN ) )            {                redirect( base_url(), "location", 303 );                    die();            }    }}class Member_Controller extends MY_Controller {    function __construct()    {        parent::__construct();            if( !$this->user->has_permissions( PERMISSION_REGISTERED ) )            {                redirect( base_url(), "location", 303 );                die();            }    }}

As you can see, all sub controllers have the widgets automatically because they extend either public, admin or member. A sub controller extending an admin controller has automatically the permissions checked so you don't need to do that ever again. You can apply this concept to your app.

A sub-controller: (placed in the normal application/controllers)

class Articles extends Member_controller {    ...}

Will have automatically ensured that the user is logged in, and the widgets are prepared without doing anything because the parent of parent class already prepared them. All you need to do in articles is to call $this->layout->render if the logic needs layout rendering at the end.


Codeigniter controllers are designed after the transaction script pattern, it's known that controller tend to get large and "not DRY" when you application grows.

To prevent such, you can re-implement the view to handle a two-step compound view pattern supporting layouts. Look for a layout view IIRC there are some on the codeigniter site.


I wrote a blog post explaining my design philosophy on organizing CodeIgniter controllers to make them more DRY. I like to have the index function of my controller serve as a common entry/exit point to avoid repeating operations that are common to all controller methods.

http://caseyflynn.com/2011/10/26/codeigniter-php-framework-how-to-organize-controllers-to-achieve-dry-principles/