Difference between revisions of "MVCnPHPController"

From GeeklogWiki
Jump to: navigation, search
(Sample Controller)
 
(MVCnPHP Controller)
Line 8: Line 8:
  
 
Ok, enough talk!  So let us create our first controller!
 
Ok, enough talk!  So let us create our first controller!
 
+
<code><pre>
/**
+
// Bring in the MVC controller  
* Bring in the MVC controller  
 
*/
 
 
require 'Geeklog/MVCnPHP/Controller.php';
 
require 'Geeklog/MVCnPHP/Controller.php';
  
Line 35: Line 33:
 
     die('Exception: ' . $e->message());
 
     die('Exception: ' . $e->message());
 
}
 
}
 +
</pre></code>

Revision as of 20:45, 19 September 2006

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!

// 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 {
    // Start output buffering so that if an exception occurs during
    // the generation of the output, it is still possible to clean
    // the buffer and regenerate the error screen instead.
    ob_start();
    
    $controller->processRequest($urlArgs);
    
    // If all is successful, output the successful view.
    ob_end_flush();    
} catch (Exception $e) {
    die('Exception: ' . $e->message());
}