[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/controller/ -> controller.class.php (sommaire)

\defgroup Controller The controller is the central piece of the MVC pattern, and the object that takes care of transferrign the process flow to the right action class based on a certain value in the incoming request.

Poids: 382 lignes (16 kb)
Inclus ou requis:0 fois
Référencé: 6 fois
Nécessite: 0 fichiers

Définit 1 class

Controller:: (8 méthodes):
  Controller()
  setActionFolderPath()
  registerAction()
  _getActionClassName()
  setForwardAction()
  loadActionClass()
  setCannotPerformAction()
  process()


Classe: Controller  - X-Ref

\ingroup Controller

This is how MVC works, using the pattern of the 'Front Controller'. With this pattern, we have
a single controller class that receives all the requests from the client, identifies the
action to be taken and the relays the execution of the action to the most suitable Action class.
The 'Action' class then will use the application business logic (the Model) to carry out the
operations necessary.

(according to http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html)

# The controller receives a POST from the client.
# The controller creates an Action corresponding to the requested operation (as described in the previous section).
# The controller calls the Action's perform method.
perform calls a model business method.
# The controller calls the screen flow manager to select the next view to display.
# The screen flow manager determines the next view and returns its name to the controller.
# The controller forwards the request to the templating service, which assembles and delivers the selected view to the client.

The Controller uses an action map file that maps action parameters to action classes. This is file is
nothing more than an associative PHP array where the key of the array is the value of the action
parameter and the key is the name of an action class. The default action parameter is "op" and the default
action is "Default" unless otherwise specified.

This is an example of a map file:

<pre>
$actions["Default"] = "AdminDefaultAction";
$actions["Login"] = "AdminLoginAction";
$actions["blogSelect"] = "AdminMainAction";
$actions["Dashboard"] = "AdminMainAction";
$actions["Manage"] = "AdminManageAction";
</pre>

It is not necesary to specify the full extension of the file, and by default it is considered to be
".class.php". In case this is not the case, please the ResourceClassLoader::setClassFileSuffix()
method.

Classes can be dynamically loaded (meaning that we
do not need to load all of them via include() or lt_include() at the top of our code), via the
ResourceClassLoader class. This class will scan a set of pre-defined folders looking for the action
class and load it if it's the one we were looking for.

In order to create a Controller object and process a request, we would use something like:

<pre>
$controller = new Controller( "myactionmap.properties.php" );
$controller->process( $_REQUEST );
</pre>

It is also possible to register new actions dynamically at run time via the static method
Controller::registerAction() which takes an action class name and an action id as parameters. This is the
method used by plugins to register new actions (although via the wrapper PluginBase::registerAction())

In our particular implementation, each action class should extend the Action class or a child of it
such as BlogAction, AdminAction, etc. Each one of this action classes should at least implement
the Action::perform() method and include there its business logic (which can be whatever) Action classes
are expected to generate a valid View class that the Controller will use to render the contents that
will be sent back to the client. The Controller will get the right view via the Action::getView() method, and
views can be set via the Action::_view attribute or the Action::setView() method.

Another method that the controller will call is the validate() method that can be used for basic
data validation logic. The base Action class already provides some code in this method, which is tied to
the FormValidator and Validator classes so in case of data validation, it is advisable to use those
methods. However if required, feel free to reimplement Action::validate(), keeping in mind that if the controller
receives a positive value, it will continue processing the Action::perform() and if not, it will stop
processing and call Action::getView() right away.

Actions can also forward the process flow to another action without generating a view by calling
the static method Controller::setForwardAction(), which takes an action class identifier as a parameter. If
after processing action the controller detects that there is a forwarding in place, it will <b>not</b>
call Action::getView() but instead, start the process again and call validate() and process() in the next
action in line.

Controller( $actionMap, $actionParam = DEFAULT_ACTION_PARAM )   X-Ref
$ActionsMap is an associative array of the form:

( $actionName, $actionClassName )

Where for every different possible value of the 'action' parameter in the request,
there is an object inheriting form the Action class that will take care of
that requested action.

param: actionMap is the associative array with the mappings
param: actionParam is the name of the parameter in the request that will be used
param: loadActionClasses By default set to 'true', enables dynamic loading of the

setActionFolderPath( $newActionFolderPath )   X-Ref
sets the folder where action classes can be dynamically loaded

param: newActionFolderPath absolute or relative path to the folder

registerAction( $actionKey, $actionClass )   X-Ref


_getActionClassName( $actionName )   X-Ref
Add function info here


setForwardAction( $forwardAction, $previousActionObject = null )   X-Ref
Sets the action to which we will forward the process

param: forwardAction Name of the action to which we will forward the current
param: previousActionObject

loadActionClass( $actionClass )   X-Ref
Loads an action class from disk. I have refactored it and put this little bit in its
own method because doing so, applications that want to load the action classes from
somewhere else than PLOG_CLASS_PATH/class/action/, or have a different naming scheme
can extend this class and reimplement this method at will.

param: actionClass The name of the action class that is to be loaded.
return: Always true.

setCannotPerformAction( $actionClass )   X-Ref
Specific controllers should use this method to set a class that will be used in case
Action::canPerform() return false. The controller will then load this class and execute
it as if it was a normal action.
This feature can be used to display a view with an error message in case our controller
and actions are working together to provide permission-based access: each action checks whether
the credentials of the current user allow him to execute the current action or not in
the Action::canPeform() method and if it returns true, then the action specified in this method
call takes over and displays whatever error message needs to be displayed (or does some
cleanup, etc, whatever needed)

param: actionClass A string with the name of the class that should be loaded when

process( $httpRequest )   X-Ref
Processess the HTTP request sent by the client

param: httpRequest HTTP request sent by the client



Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics