Blog app #2: the controller

Published on Tuesday 26 April 2016. Tagged as PHP.

In a model-view-controller concept, the controller is the part that handles user requests, instructs the model to update or retrieve data. When something has to be displayed, the controller handles the data to the view, which then renders it.

The BlogController class

The BlogController is the controller in the MVC pattern, processing the different requests. It never acts with the data storage directly - this is the domain of the 'model' - nor any rendering - the domain of the 'view'. It merely acts as a router. The most simple router possible is based on naming conventions. For each main request, there's a corresponding method. This class can be found in sys/classes/blogcontroller.php.

The constructor method

The configuration from content/config.php is passed to the constructor:

function __construct($config){
	$this->config=$config;

Next, the request is split into its components and the top active elements for 'post' and 'page' are resolved:

	$this->request=explode( '/', ( isset( $_GET['url'] )?$_GET['url']:'index' ).'////' );
		$here=$this->request[0];
		switch( $here ){
			case 'post': $here='index'; break;
			case 'page': $here=$this->request[1]; break;
		}

The config variables are extracted. Then the 'data' object is initiated. This is the object that will be passed to the view. Finally, the 'model' object is initiated. This will act as the gateway to all stored data.

	extract($config);
	$this->data=array( 'app'=>$app, 'request'=>$this->request, 'template'=>$template, 'menu'=>$menu, 'topActive'=>$here );
	$this->model=new BlogModel( $db );

The handleRequest method

This method is called directly when the class is created and, as the name says, handles the requests. First, the method name to call is calculated based on the first part of the request. This is always a 'get' request, e.g. the request page/about is translated to the method getPage():

function handleRequest(){
	$fn='get'.ucfirst( $this->request[0] );

Then a call is made to that method to prepare de content. If the method does not exist, a call is made to get404() instead:

	call_user_func( array( $this, method_exists( __CLASS__, $fn )?$fn:'get404' ) );

Finally, a new ViewPage class is created and the 'data' object passed to it. Then it's render() method is called with the name of the template to use. This will generate an HTML page. Note: in the display part of the Blog app, there are only 'get' requests to solve. In the admin part, there will be 'get' and 'post' request. So for now, the response to the requests are always HTML pages:

	call_user_func( array( new ViewPage( $this->data ), 'render' ), $this->data['template'] );

The getIndex method

As an example of how requests are handled: this method is called to retrieve the list of recently published posts on the home page. First, the list is requested from the 'model', then the extra info is added to the 'data' object:

function getIndex(){
	$obj=$this->model->getPosts( (int) $this->data['request'][1] );
	$this->extendData( array( 'type'=>'index', 'title'=>'Recent posts', 'content'=>$obj['list'], 'pagination'=>$obj['pagination'] ) );
}

Next? In a next post, I'll explain how the BlogModel class uses PDO to retrieve data from the MySql database.

The Blog app project on GitHub