Understanding the Controller of MVC programming in PHP

Source: Internet
Author: User
Tags getmessage joe stump net pear valid
Programming | Control simply, the role of the controller is to accept the request. It uses the obtained method, where the URI is used to load a function module to refresh or submit a presentation layer. The controller will use the $_get automatic global variable to determine which module is loaded.

An example of a request that looks like this:

Http://example.com/index.php?module=login

This looks simple, but not in the process of implementation. Here are some argument parts that the controller can identify:

module defines which modules, such as the Users module, are used
class defines which functional class to use, such as whether you want the user to login or logout
An event defines which specific event to use

Such a more complex example could explain the final request URL for each of the above argument:

Http://example.com/index.php?module=users&class=login

This request tells the Controller to load the users module, then the login class, and then runs the Login::__default () default event because no specific event is defined.

Here is the specific Code section:

<?php
/**
* index.php
*
* @author Joe Stump <joe@joestump.net>
* @copyright Joe Stump <joe@joestump.net>
* @license http://www.opensource.org/licenses/gpl-license.php
* @package Framework
*/

Require_once (' config.php ');

{{{__autoload ($class)
/**
* __autoload
*
* AutoLoad is ran by PHP, it can ' t find a class it are trying to load.
* By naming we classes intelligently we should be able to load most classes
* Dynamically.
*
* @author Joe Stump <joe@joestump.net>
* @param string $class class name we ' re trying to load
* @return void
* @package Framework
*/

function __autoload ($class)
{
$file = Str_replace (' _ ', '/', substr ($class, 2)). PHP ';
Require_once (Fr_base_path. ') /includes/'. $file);
}
// }}}

if (Isset ($_get[' module ')) {
$module = $_get[' module '];
if (Isset ($_get[' event ')) {
$event = $_get[' event '];
} else {
$event = ' __default ';
}

if (Isset ($_get[' class ')) {
$class = $_get[' class '];
} else {
$class = $module;
}

$classFile = Fr_base_path. ' /modules/'. $module. ' /'. $class. '. PHP ';
if (file_exists ($classFile)) {
Require_once ($classFile);
if (class_exists ($class)) {
try {
$instance = new $class ();
if (!fr_module::isvalid ($instance)) {
Die ("requested module is not a valid framework module!");
}

$instance->modulename = $module;
if ($instance->authenticate ()) {
try {
$result = $instance-> $event ();
if (! Pear::iserror ($result)) {
$presenter = Fr_presenter::factory ($instance->presenter, $instance);

if (! Pear::iserror ($presenter)) {
$presenter->display ();
} else {
Die ($presenter->getmessage ());
}
}
catch (Exception $error) {
Die ($error->getmessage ());
}
} else {
Die ("You don't have access to the requested page!");
}
catch (Exception $error) {
Die ($error->getmessage ());
}
} else {
Die ("A valid module for your request is not found");
}
} else {
Die ("Could not find: $classFile");
}
} else {
Die ("A valid module is not specified");
}

? >

[1] [2] Next page



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.