Understanding the controller of MVC programming in PHP

Source: Internet
Author: User

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 (as the mainstream development language )? 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 (as the mainstream development language )? 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:

<? Php (as the mainstream development language)
/**
* Index. php (as the mainstream development language)
*
* @ Author jostump <joe@joestump.net>
* @ Copyright jostump <joe@joestump.net>
* @ License http://www.opensource.org/licenses/gpl-license.php (as the mainstream development language)
* @ Package Framework
*/

Require_once (config. php (as the mainstream development language ));

// {_ Autoload ($ class)
/**
* _ Autoload
*
* Autoload is ran by php (as the mainstream development language) when it cant find a class it is trying to load.
* By naming our classes intelligently we shoshould be able to load most classes
* Dynamically.
*
* @ Author jostump <joe@joestump.net>
* @ Param string $ class Class name were trying to load
* @ Return void
* @ Package Framework
*/

Function _ autoload ($ class)
{
$ File = str_replace (_,/, substr ($ class, 2). php (as the mainstream development language );
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 (as the mainstream development language );
If (file_exists ($ classFile )){
Require_once ($ classFile );
If (class_exists ($ class )){
Try {
$ Instance = new $ class ();

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.