Implementing the MVC Controller in PHP _php tutorial

Source: Internet
Author: User
Tags joe stump pear
The controller "controllers"

Simply put, the function of the controller is to accept the request. It uses the obtained method, where a function module is loaded through a URI to refresh or submit a presentation layer. The controller uses the $_get auto global variable to determine which module to load.
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 of the argument parts that the controller can identify:
module defines which modules to use, such as the Users module
class defines which feature class to use, such as whether you want the user to login or logout
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 finally runs the Login::__default () default event because there is no specific event defined.
Here is the specific Code section:

/**
* index.php
*
* @author Joe Stump
* @copyright Joe Stump
* @license http://www.opensource.org/licenses/gpl-license.php
* @package Framework
*/

Require_once (' config.php ');

{{{__autoload ($class)
/**
* __autoload
*
* Autoload is-ran by PHP when it can ' t find a class It's trying to load.
* By naming our classes intelligently we should is able to the load most classes
* Dynamically.
*
* @author Joe Stump
* @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 was 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) {
&nb

http://www.bkjia.com/PHPjc/508490.html www.bkjia.com true http://www.bkjia.com/PHPjc/508490.html techarticle Thecontroller "Controller" simply speaking, the function of the controller is to accept the request. It uses the Get method, here is through URI, load a function module to refresh or commit a ...

  • 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.