Implementing the controller of MVC in PHP

Source: Internet
Author: User
The & nbsp; Controller [Controller] in short, The Controller is used to accept requests. It uses the obtained method. here, a function module is loaded through URI to refresh or submit a presentation layer. The controller uses the $ _ GET automatic global variable to determine which module to load. For a request example, see SyntaxHighlighter. all The Controller [Controller]

Simply put, the controller is used to accept requests. It uses the obtained method. here, a function module is loaded through URI to refresh or submit a presentation layer. The controller uses the $ _ GET automatic global variable to determine which module to load.
An example of a request looks like this:
Http://example.com/index.php? Module = login
This seems simple, but not in the implementation process. Here is the argument part that several controllers can recognize:
Module defines which module to use, such as the users module
Class defines which function class to use, such
Event defines which specific event to use

In this more complex example, we can explain the final request URL composed of each argument:
Http://example.com/index.php? Module = users & class = login
This request tells the controller to load the users module, followed by the login class, and finally runs the default login :__ default () event because no specific event is defined.
The code is as follows:

/**
* 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 is trying to load.
* By naming our classes intelligently we shoshould be able to load most classes
* Dynamically.
*
* @ Author Joe Stump
* @ Param string $ class Class name we're 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 ){
& Nb

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.