MVCnPHPConfig

From GeeklogWiki
Jump to: navigation, search

MVCnPHP Configuration File

Ok, so you've built your first view using MVCnPHP but now you need to configure the controller to call your view when requested. This is done by managing a very simple XML file. Back when we created our index.php page that instantiated the controller we gave it a file called mvcconfig.xml as the configuration it should use. You should now create that XML file and add the following:

<?xml version="1.0" encoding="UTF-8"?>
<mvc_configuration compile="true">	
    <view id="home" name="SAMPLE_HomeView" default="true" />    
</mvc_configuration>

The above configuration file is about as bare bones as it gets. The first tag, MVC_CONFIGURATION, has a compile attribute that will tell MVCnPHP it should compile the XML to a PHP array for faster processing. It's generally a good idea to do this. The VIEW tag defines a view for the controller with a logical ID of home and a class name of SAMPLE_HomeView. The default attribute tells the controller that if it isn't given the ID of a view that it should use that view as the default. So how do you tell the controller what view to use?

http://www.example.com/index.php?cmd=home

The above shows you that you can call a specific view by giving it a cmd GET string variable. Note that because our configuration file has the home view as the default view the above URL is the same as:

http://www.example.com/index.php

Some of you are probably grumbling about the use of the ugly GET string and noting how URL rewriting would be better. You are in luck as MVCnPHP will treat the following URL the same as the above:

http://www.example.com/index.php/home/

No, MVCnPHP doesn't use Apache's URL rewriting...instead it chooses to use a hosting provider friendly implementation that parses the GET string and it assumes the first argument is the cmd value.

For reference, this what the compiled configuration file would look like. Don't worry if it looks a bit foreign as that array is built for direct use by the Contoller. Note that if you set the compile attribute to false on the MVC_CONFIGURATION tag that the controller would have to build that array on every request...not a good thing.

<?php $this->arrayData = array (
  'views' => 
  array (    
    'home' => 
    array (
      'name' => 'SAMPLE_HomeView',
      'default' => 1,
    )
  )
);
?>

Ok, so now we've created our first view, its associated Flexy template and wired it into the controller's configuration file. Now let's build a command!