print Tutorial 2: Pulling additional view information

The guestbook controller

The first step in the process is to setup our guestbook controller on the customer facing site so that additional requests can be made to it for specific views. These requests will be rendered using whatever the request context is.

Custom views are added via the initialise() method. Locate this method and the existing getControllerActions(). We will set-up the views below this.

Additional views are held in the mvcControllerViews object that can be fetched using getControllerViews(). Similar to the mvcControllerActions, this has a "fluent" interface allowing the setting of views to be chained together.

We need to add:

//guestbookController.class.php
/*
 * Register permitted standalone views 
 */
$this->getControllerViews()->addView('top5');

This will register the view "top5" as an allowed view to be called by includeView or getControllerView(). A specific hardcoded name is not as useful as a class constant, so define a new constant near to the existing ACTION_ constants:

const VIEW_TOP5 = 'top5';

Update the previous code to reference the constant.

So we have a named view, how do we allow this to be pulled from the controller?

All view requests are passed to a method called fetchStandaloneView(). This accepts an array of parameters one of which must be named 'view'. This method will need to be created and configured for the guestbook controller:

/**
 * @see mvcControllerBase::fetchStandaloneView()
 */
function fetchStandaloneView($params) {
	switch ( $params['view'] ) {
		case self::VIEW_TOP5:
			$oView = new guestbookView($this);
			return $oView->getStandaloneTop5View();
		break;
	}
	return '';
}

Above we have added the method and defined that the view 'top5' will fetch a specific view from the guestbookView object. Note how a string is returned, even from the view: there is no direct display, the output is returned to the calling template.

And that's all the changes that need to be made to the controller. Next up are the model and view.