Welcome Guest, Not a member yet? Register   Sign In
MY_Controller extended Controller class
#1

[eluser]Jamie Rumbelow[/eluser]
This is an extended Controller class to use in CodeIgniter applications that provides some clever model loading, view loading and layout support. It uses a Rails-like convention system to automatically load the view based on the controller name and the current method, and uses a custom layout system.

Usage
Drag the MY_Controller.php file into your application/libraries folder. CodeIgniter will load and initialise this class automatically for you. Then all you have to do is make sure all your controllers extend from MY_Controller and all the functionality will be baked into your controllers automatically!

Tutorial
I've written a tutorial that takes a reasonable look at the functionality provided by the entire class, as well as my MY_Model base CRUD model on my delightful blog. Check it out and feel free to leave a comment.

Download
You can get the source directly from GitHub, where I'll be updating the library. You can also fork it and customise it yourself! Alternatively, you can download it as a .zip or a .tar.

View Loading
Depending on the controller and action running, this class will try to guess the location of the view and load it for you. To pass data through to the view you simply set the $this->data array in a similar fashion to normal and it will be passed through to the view. For example, if we were in the Games controller and the index() action, the class would try to load the application/views/games/index.php view.

If you need to customise this view loading convention in any way, you can by setting the $this->view instance variable anywhere in your controller. Set this to any view and that view will be loaded, or set it to FALSE to not load a view at all. It's very flexible this way.

Layout Loading
This class will also try to load your view into a layout. By default, it will first look for a layout file (located in your application/views/layouts folder) with the same name as the current controller. If it can't find that it will then look for application/views/layouts/application.php - a global, application level layout file. For example, if you had an Admin controller it will first look for application/views/layouts/admin.php and then if that doesn't exist, it will try to load application/views/layouts/application.php layout instead.

You can also override this functionality by setting the $this->layout instance variable. Set it to a string to load that layout, or FALSE to load no layout. This will take precedence over the conventional loading strategy.

Model Loading
Set the $this->models instance variable in your controller to be an array of all the models you want to load and this class will loop through that array, loading each one sequentially. It looks for the model with a filename of $model_name_model.php and the class name of $model_name_model, but will load the model into the CI super object under $model_name. For instance, if my $this->models array had a 'game' model, it would look for the file and class Game_model but will load it to $this->game.

You can customise the suffix or prefix of the model name by setting the $this->model\_string variable. Use the % symbol, which will be replaced with the model you want to load.

Custom 404 Messages
If you try to call a method that doesn't exist, the class will first look for a \_404 method in your controller. This method will be passed the method that you're trying to call. Otherwise, it will display a default CodeIgniter 404 message.

Upcoming Features
* More customisable options
* Custom model names
* Automagic Library Loading

Version History
* 1.2.0 A bugfix, support for custom model names and custom 404 messages.
* 1.1.0 Partial support, asides and an improvement to documentation
* 1.0.5 Added support for multiple view rendering.
* 1.0.2 A few bugfixes
* 1.0.0 First release of library and basic functionality, with basic readme


Messages In This Thread
MY_Controller extended Controller class - by El Forum - 12-02-2009, 03:17 AM
MY_Controller extended Controller class - by El Forum - 12-03-2009, 05:27 AM
MY_Controller extended Controller class - by El Forum - 12-03-2009, 06:13 AM
MY_Controller extended Controller class - by El Forum - 12-03-2009, 10:24 AM
MY_Controller extended Controller class - by El Forum - 12-03-2009, 10:30 AM
MY_Controller extended Controller class - by El Forum - 12-04-2009, 02:31 AM
MY_Controller extended Controller class - by El Forum - 12-17-2009, 11:33 AM
MY_Controller extended Controller class - by El Forum - 01-06-2010, 11:04 PM
MY_Controller extended Controller class - by El Forum - 03-31-2010, 11:08 AM
MY_Controller extended Controller class - by El Forum - 04-02-2010, 06:53 PM
MY_Controller extended Controller class - by El Forum - 04-02-2010, 07:32 PM
MY_Controller extended Controller class - by El Forum - 04-03-2010, 03:43 AM
MY_Controller extended Controller class - by El Forum - 07-12-2010, 05:21 AM
MY_Controller extended Controller class - by El Forum - 07-12-2010, 08:44 AM
MY_Controller extended Controller class - by El Forum - 10-03-2010, 08:57 AM



Theme © iAndrew 2016 - Forum software by © MyBB