Home php教程 php手册 理解PHP中的MVC编程之控制器

理解PHP中的MVC编程之控制器

Jun 21, 2016 am 09:11 AM
class module php

编程|控制

    简单来讲,控制器的作用就是接受请求。它使用获取的方法,在这里是通过URI,载入一个功能模块来刷新或者提交一个表述层。控制器将使用$_GET自动全局变量来判断载入哪一个模块。

  一个请求的例子,看起来像这样:

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

  这看起来很简单,但是在实现的过程中却不是。这里是几个控制器能识别的argument部分:

  module定义了使用哪一个模块,如users模块
  class定义了使用哪一个功能类,如你想让用户login还是logout
  event定义了使用哪一个具体事件

  这样一个更复杂的例子可以解释上面的各个argument最终组成的请求URL:

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

  这段请求告诉控制器应该载入users模块,然后是login类,最后因为没有定义具体事件,所以运行login::__default()默认事件。

  以下是具体代码部分:

<?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 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 <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 do not have access to the requested page!");
   }
  } catch (Exception $error) {
   die($error->getMessage());
  }
 } else {
  die("An valid module for your request was not found");
 }
 } else {
  die("Could not find: $classFile");
 }
 } else {
  die("A valid module was not specified");
}

?>

[1] [2] 下一页  



Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles