Our first controller

From Rhypedigital

Jump to: navigation, search

Our first controller creates the view and asks it to render itself.

It expects a database object but at this time we're just passing in null.


<?php
 
#
#  File     : Ctrl/Message.php
#  Copyright: 2009
#           : Rhype Digital Ltd
#  License  : This file is released under the terms of the Rhype Digital Software Licence.
#           : Use of this software is prohibited without express permission of Rhype
#           : Digital Ltd.
#  EMail    : copyright@rhypedigital.com
#
 
require_once   ('Lib/View/Message.php'       ) ;
require_once   ('RiPHPLib/Support/Class.php' ) ;
require_once   ('RiPHPLib/MVC/View.php'      ) ;
 
#  This is the controller class. By convention the class is named Ctrl_name
#  where name is the controller name (in this case Message).
#
class Ctrl_Message extends RPL_Support_Class
{
   protected   $m_dbh         ;
   protected   $m_cgi         ;
 
   #  Class constructor, typically stores the database and CGI objects for
   #  later use.
   #
   function __construct ($dbh, $cgi)
   {
      $this->m_dbh         = $dbh   ;
      $this->m_cgi         = $cgi   ;
   }
 
   #  There should be a method for each action, with the same name as the
   #  action. Typically, init is the default action.
   #
   function init ()
   {
      #  Create an instance of the view which will render the page, in this
      #  example passing the message text. This is then rendered and the
      #  result returned.
      #
      $msg     = $this->m_cgi->param  ('message', 'Welcome to Blunders') ;
      $view    = new Lib_View_Message ($msg) ;
      return   $view->render () ;
   }
}
 
#  A controller should have a function named name_Factory where name is the
#  controller name (in this case Message). This is called from the RPL_MVC_Controller
#  instance created in the main "index.php" script, and passes through the database
#  and CGI objects. The function creates an instance of the controller class.
#
function Message_factory ($dbh, $cgi)
{
   return   new Ctrl_Message ($dbh, $cgi) ;
}
?>

As you can see, the page will accept a parameter called message and display whatever you pass in. It will default to 'Welcome to Blunders'.

Personal tools