MVCnPHPController

From GeeklogWiki
Revision as of 13:59, 21 September 2006 by Tony (talk | contribs) (MVCnPHP Controller)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

MVCnPHP Controller

The MVCnPHP Controller is the heart of MVCnPHP. Every controller you create using MVCnPHP is responsible for controlling program execution to the views and commands it is configured for. It is not uncommont to have multiple controllers in one application and, in fact, on larger applications it is recommended. How you decide to partition out multiple controllers is complete up-to-you but there are some examples that might help:

  1. Plugin-based Applications. A plugin architecture is a prefect place to implement multiple controllers as it would allow each plugin to define their own set of of controllers outside of the host application.
  2. Security Conscious Applications. If your application has a number of administrative screens, you may decide to have all administrative views and commands to be handled by a controller separate from the rest of the application. In fact, if you do this you can even use network wizardy for enhanced security like using proxy servers to lock down access to, say mysite.com/admin to a subnet while allow access to mysite.com/ to the entire internet!
  3. Very Large Applications. If you get a PHP application that has a large number of views and command you may find that using multiple controllers will help with basic organization.

Ok, enough talk! So let us create our first controller! Create a new page, say, index.php that will reside in your application's document root.

// Bring in the configuration file
require '/path/to/my/config.php';

// Bring in the MVC controller 
require 'Geeklog/MVCnPHP/Controller.php';

// Instantiate the controller
$configFile = $conf['path'] . 'mvcconfig.xml';
$controller = new MVCnPHP_Controller($configFile);
$controller->setBaseURL($conf['site_url']);
$controller->setViewDir($conf['path_views']);
$controller->setCommandDir($conf['path_commands']);

// Now have the controller do some work
try {
    $controller->processRequest($urlArgs);
} catch (Exception $e) {
    die('Exception: ' . $e->message());
}

Let's quickly run through what is going on here.

// Bring in the configuration file
require '/path/to/my/config.php';

The above simple brings in my config.php that would have amongst other things in it the following $config array:

$conf['site_url'] = 'http://www.example.com';
$conf['path'] = '/path/to/my/application/';
$conf['path_views'] = $conf['path'] . 'views/';
$conf['path_commands'] = $conf['path'] . 'commands/';

The next require brings in the code for the MVCnPHP controller. Note that we assume that year PEAR directory is in your php.ini's include_path:

require 'Geeklog/MVCnPHP/Controller.php';

Then we have to configure and instantiate the controller:

$configFile = $conf['path'] . 'mvcconfig.xml';
$controller = new MVCnPHP_Controller($configFile);

mvcconfig.xml is the controller configuration file we are handing to the controller we about to instantiate. For now you can safely ignore what that file is as we cover it in great detail later. As you see, we pass the location of the configuration file to the controller's constructor.

After that there are a few options you'll want to set:

$controller->setBaseURL($conf['site_url']);

The above gives the base URL which is then handed down to the various commands and views that are called upon. The site url is then used to build any URL reference for things like anchor and image tags.

$controller->setViewDir($conf['path_views']);
$controller->setCommandDir($conf['path_commands']);

The above wraps up the MVCnPHP controller setup by specifying where on the file system the controller can find the various commands and views it will need to call upon. After that it is a matter of simply telling the contoller to process the incoming request:

try {
    $controller->processRequest($urlArgs);
} catch (Exception $e) {
    die('Exception: ' . $e->message());
}

Note how we catch any unhandled exceptions and simply echo the exception message to the screen. You could very easily replace all that with code to do some custom logging and special handling.

What we have now is a single file, index.php, that instantiates one controller and then processes the incoming request. You have to be wondering how the controller knows what to do, right? That part is completely handled by the MVCnPHP configuration file which we'll cover in detail shortly. Before we do that, let us create a view.