With the admin system pretty much sorted, we can now look at building our front-facing guestbook controller. For this tutorial I am going to add it to the base site for the sake of simplicity.
Back to the command line, and then run the following:
php scorpio.php new controller guestbook --site=base
This will create a new controller in the "base" site called "guestbook". We will need to add additional actions to allow for posting of new comments. Actions are always constants and should be set at the head of the controller class. There is already an existing action "view". Add under this an action for "post" and then another action for "doPost".
class guestbookController extends mvcController {
const ACTION_VIEW = 'view';
const ACTION_POST = 'post';
const ACTION_DO_POST = 'doPost';
}
Now add these new actions to the set of permitted actions. This is done via the initialise() call. You can see the existing "view" action has already been assigned. The mvcControllerActions object supports a "fluent" interface (the object is returned after a set call) so you can chain the addAction() methods together:
$this->getControllerActions()
->addAction(self::ACTION_VIEW)
->addAction(self:ACTION_POST)
->addAction(self::ACTION_DO_POST);
Alternatively, add them individually if you prefer that syntax.
$this->getControllerActions()->addAction(self::ACTION_VIEW); $this->getControllerActions()->addAction(self:ACTION_POST); $this->getControllerActions()->addAction(self::ACTION_DO_POST);
In the main launch() method, logic needs to be added to handle a post and doPost request. For the sake of brevity the finished code looks something like the following. It is left to the reader to improve and understand what is here.
class guestbookController extends mvcController {
const ACTION_VIEW = 'view';
const ACTION_POST = 'post';
const ACTION_DO_POST = 'doPost';
/**
* @see mvcControllerBase::__construct()
*/
function __construct($inDefaultAction = self::ACTION_VIEW) {
parent::__construct($inDefaultAction);
$this->setRequiresAuthentication(false);
}
/**
* @see mvcControllerBase::initialise()
*/
function initialise() {
parent::initialise();
$this->getControllerActions()
->addAction(self::ACTION_VIEW)
->addAction(self::ACTION_POST)
->addAction(self::ACTION_DO_POST);
$this->addInputFilters();
}
/**
* @see mvcControllerBase::launch()
*/
function launch() {
switch ( $this->getAction() ) {
case self::ACTION_DO_POST:
try {
$data = $this->getInputManager()->doFilter();
$this->addInputToModel($data, $this->getModel());
$this->getModel()->save();
header("Location: ".$this->buildUriPath(self::ACTION_VIEW));
exit;
} catch ( Exception $e ) {
systemLog::error($e->getMessage());
$this->setAction(self::ACTION_POST);
$this->getModel()->setErrorMessage($e->getMessage());
}
break;
}
$oView = new guestbookView($this);
if ( $this->getAction() == self::ACTION_POST ) {
$oView->showGuestbookPostPage();
} else {
$oView->showGuestbookPage();
}
}
/**
* @see mvcControllerBase::addInputFilters()
*/
function addInputFilters() {
$this->getInputManager()->addFilter('Name', utilityInputFilter::filterString());
$this->getInputManager()->addFilter('EmailAddress', utilityInputFilter::filterString());
$this->getInputManager()->addFilter('Comment', utilityInputFilter::filterString());
}
/**
* @see mvcControllerBase::addInputToModel()
*/
function addInputToModel($inData, guestbookModel $inModel) {
$inModel->setName($inData['Name']);
$inModel->setEmailAddress($inData['EmailAddress']);
$inModel->setComment($inData['Comment']);
$inModel->setIpAddress($_SERVER['REMOTE_ADDR']);
$inModel->setReferrer($_SERVER['HTTP_REFERER']);
}
}
The generated model has been changed slightly. It now extends from the guestbook object, and adds an "error message" property:
class guestbookModel extends guestbook {
protected $_ErrorMessage = '';
function getErrorMessage() {
return $this->_ErrorMessage;
}
function setErrorMessage($inError) {
$this->_ErrorMessage = $inError;
return $this;
}
}
Finally the view needs assignment of data from the model:
class guestbookView extends mvcView {
/**
* @see mvcViewBase::__construct()
*/
function __construct($inController) {
parent::__construct($inController) ;
}
/**
* Shows the guestbookView page
*
* @return void
*/
function showGuestbookPage() {
$this->setCacheLevelNone();
$this->getEngine()->assign('entries', utilityOutputWrapper::wrap(guestbook::listOfObjects()));
$this->getEngine()->assign('postURI', $this->getController()->buildUriPath(guestbookController::ACTION_POST));
$this->render($this->getTpl('guestbook'));
}
/**
* Shows the guestbookPost page
*
* @return void
*/
function showGuestbookPostPage() {
$this->setCacheLevelNone();
$this->getEngine()->assign('oModel', utilityOutputWrapper::wrap($this->getModel()));
$this->getEngine()->assign('doPostURI', $this->getController()->buildUriPath(guestbookController::ACTION_DO_POST));
$this->render($this->getTpl('postMessage'));
}
}
This should cover most of the logic needed to support the guestbook. The last elements are the two templates that we need to show entries and to allow a new entry to be created.
Posted by: Dave Redfern (Writer), in Tutorials on 12 Apr 2008 @ 13:51
Tags: dao, generator, guestbook, mvcgenerator, scorpio, tutorial,
Contents:
Related Articles
This
work is licenced under a
Creative Commons Licence.