Home >Backend Development >PHP Tutorial >Implementing the MVC Controller in PHP_PHP Tutorial

Implementing the MVC Controller in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:34:25764browse

The Controller 【Controller】

Simply put, the role of the controller is to accept requests. It uses the get method, in this case via URI, 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 to load.
An example request would look like this:
http://example.com/index.php?module=login
This seems simple, but in implementation it is not. Here are several argument parts recognized by controllers:
module defines which module to use, such as users module
class defines which functional class to use, such as whether you want the user to log in or logout
event defines which specific event to use

Such a more complex example can explain the request URL that is ultimately composed of each of the above arguments:
http://example.com/index.php?module=users&class=login
This request tells the controller that the users module should be loaded, then the login class, and finally because no specific event is defined, the login::__default() default event is run.
The following is the specific code part:

/**
  * 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 should be able to 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 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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/508490.htmlTechArticleTheController 【控制器】 简单来讲,控制器的作用就是接受请求。它使用获取的方法,在这里是通过URI,载入一个功能模块来刷新或者提交一...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn