Development Information

August 22, 2010

Mini MVC – How to Use – The Base Controller

Filed under: Mini MVC Information,Tutorials — judda @ 1:33 pm

Sorry for the horribly long delay but here is a quick run down on how to extend the BaseController to write your personal controllers.

As mentioned before, the controller acts as the liaison between the model and the view. These are the classes which get looked for and called whenever your site is visited. For example, if your site is http://www.site.com and the URL is http://www.site.com/about, then Mini MVC will automatically look for the controller named ‘AboutController’ in the ‘./app/controllers/’ directory. The class name for each controller should have the name ‘Controller’ at the end, in order to prevent any namespace collisions between the controllers and the models. It also allows you to quickly and easily determine the difference between the two.

For each controller, you can have as many methods as you would like. These methods are also accessible through the web depending on the URL. For example, http://www.site.com/about/contact would call the ‘contact’ function within the ‘about’ controller. If no function name is provided in the URL, then the default function which is called will be used. This is defined in the configuration file (./app/config/config.php) as the __DEFAULTFUNCTION__. The default function which is called in ‘index’.

Please Note: if you don’t want a function to be web-accessible, prefix it with __ and it will be automatically ignored.

When making your own controller, you should always extend the base class (BaseController) because it provides a bit of additional functionality which is not available if you only make it extend the base class. For example, when you are using a model in your controller, you can then use

$this -> __loadModel ( 'ModelName' );

to load the model into memory and then access it directory through the instance of the controller (i.e. $this -> ModelName).

A sample controller will look like this:

<?php
class SampleController extends BaseController // Accessible by the web through http://www.site.com/sample
{
    public function __construct ()
    {
        parent::__construct (); // Always call the parent's constructor to initialize all information
    }
 
    public function index () // Will be called if no function name is provided (i.e. http://www.site.com/sample)
    {
         // In here you will do any of your data manipulation or generation
         // and tell it what views and models to use
         Base::__loadViews ( array (
                 // Define the 3 parts of the view (header, body and footer) - these definitions are not necessary but they are useful
                 Base::VIEW_HEADER => array ( 'header.php' /* The view name(s) to use for the header */ ),
                 Base::VIEW_BODY => array ( 'body.php' /* The view name(s) to use for the body */ ),
                 Base::VIEW_FOOTER => array ( 'footer.php' /* The view name(s) to use for the footer */ )
             ),
                 // Any variables to send in
                 array (
                     // Please Note: All variables MUST have a name associated with them.
                     'key' => 'value', // Variables to be accessible to all views (accessible through $key in the view)
                     Base::VIEW_HEADER => array ( 'Title' => 'Welcome' ) // Variables only accessible to the header view (can be done for the body and footers as well)
             )
         );
    }
 
    // The $username variable will be filled in with the value associated with information after the function name
    public function other ( $username ) // Will be called if no function name is provided (i.e. http://www.site.com/sample/other/judda)
    {
         // i.e. http://www.site.com/sample/other/judda => $username = 'judda'
         // In here you will do any of your data manipulation or generation
         // and tell it what views and models to use
         Base::__loadViews ( array (
                 // Define the 3 parts of the view (header, body and footer) - these definitions are not necessary but they are useful
                 Base::VIEW_HEADER => array ( 'header.php' /* The view name(s) to use for the header */ ),
                 Base::VIEW_BODY => array ( 'body.php' /* The view name(s) to use for the body */ ),
                 Base::VIEW_FOOTER => array ( 'footer.php' /* The view name(s) to use for the footer */ )
             ),
                 // Any variables to send in
                 array (
                     // Please Note: All variables MUST have a name associated with them.
                     'key' => 'value', // Variables to be accessible to all views (accessible through $key in the view)
                     Base::VIEW_HEADER => array ( 'Title' => 'Welcome',
                         'name' => $username ) // Variables only accessible to the header view (can be done for the body and footers as well)
             )
         );
    }
}

That is all I can think about for this topic, please feel free to ask any questions about it so I can expand on this tutorial. :)

Form Builder (Mini MVC)

Filed under: Mini MVC Information,Updates — judda @ 1:12 am

I have completed a preliminary version of a form builder. It will allow you to programmatically build forms which can be send in from the controllers to your views so that you never have to worry about different views handling the information in different ways (or changing names or anything).

I will be posting links to the documentation soon so that all of the built-in features will be displayed. Just because you are doing things programmatically doesn’t mean you can’t stylize nor have valid HTML. You can add as many attributes to the input types as you want (including specialized ones for adding CSS classes and styles).

Please Note: In this version of the form builder, arrays of fields are not permitted (i.e. they will overwrite each other). I am actively working to remove this limitation as soon as possible.

Along with the form builder, I have uploaded a small update to the Mini MVC base engine (just a little clean up thing).

Powered by WordPress