Base Controllers

Bonfire ships with several controllers for you to extend when making your application.  Which one you use depends completely on where you are using it and what you need to accomplish.  Let’s take a quick look at the different controllers and their suggested uses.

Summary
Base ControllersBonfire ships with several controllers for you to extend when making your application.
Base_ControllerAll of the custom controllers extend from the Base_Controller.
Front_ControllerThe Front_Controller is intended to be used as the base for any public-facing controllers.
Authenticated_ControllerThis controller forms the base for the Admin Controller.
Admin_ControllerThe final controller sets things up even more for use within the Admin area of your site.
ConclusionWith all of this being loaded, won’t it affect performance? 

Base_Controller

All of the custom controllers extend from the Base_Controller.  This class extends the MX_Controller which gives you all of the power of WireDesign’s [HMVC](https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home) available to all of your classes.  That allows for a different way of working, but also a very powerful one.  We won’t go into that here.

This is the place that you want to setup anything that should happen for every page of your site...

  • Setup environment-specific settings, like turning the profiler on for development and off for production and testing.
  • Get the cache setup correctly.  This is currently setup to only use a file-based cache, but you can easily tell it to use APC if available, and fallback to the file system if not.
  • This controller also sets up System Events that will get executed just before and just after the Base_Controller’s constructor runs.  We’ll cover Events in another post.

Front_Controller

The Front_Controller is intended to be used as the base for any public-facing controllers.  As such, anything that needs to be done for the front-end can be done here.

Currently, it simply sets the active theme.  You could also set the default theme here, if you create a parent theme ‘framework’ to use with all of your sites that you extend with child themes.

Authenticated_Controller

This controller forms the base for the Admin Controller.  It was broken into two parts in case you needed to create a front-end area that was only accessible to your users, but that was not part of the Admin area and didn’t share the same themes, etc.  All changes you make here will affect your Admin Controller’s though, so use with care.  If you need to, reset the values in the Admin Controller.

This controller currently...

  • Loads in all of the user-associated models, like the user_model, permission_model, etc
  • Restricts access to only logged in users
  • Gets form_validation setup and working correctly with HMVC.
  • You might extend this controller by storing the current user in a variable that all child classes can access for ease of use.

Admin_Controller

The final controller sets things up even more for use within the Admin area of your site.  That is, the area that Bonfire has setup for you as a base of operations.  It currently...

  • Loads the pagination libraries and sets them up for a consistent user experience.
  • Gets the admin theme loaded and makes sure that some consistent CSS files are loaded so we don’t have to worry about it later.

Conclusion

With all of this being loaded, won’t it affect performance?  Well, to a very small extent, yes.  There are always trade-offs to be made between convenience and speed.  In this case, the trade-off is a little bit of extra memory usage.  All of the controllers are automatically loaded, and they’re all contained in a single file, so there isn’t the performance hit of extra files being loaded in.

The hope is that having the base controllers available is a simple way to speed up your development time while making a minimal impact on performance.

Close