日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php中的控制器是什么意思,理解PHP中的MVC编程之控制器_php

發布時間:2023/12/10 php 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php中的控制器是什么意思,理解PHP中的MVC编程之控制器_php 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡單來講,控制器的作用就是接受請求。它使用獲取的方法,在這里是通過URI,載入一個功能模塊來刷新或者提交一個表述層??刂破鲗⑹褂?_GET自動全局變量來判斷載入哪一個模塊。

一個請求的例子,看起來像這樣:

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

http://www.gaodaima.com/45695.html理解PHP中的MVC編程之控制器_php

這看起來很簡單,但是在實現的過程中卻不是。這里是幾個控制器能識別的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]?下一頁

歡迎大家閱讀《理解PHP中的MVC編程之控制器_php》,跪求各位點評,若覺得好的話請收藏本文,by 搞代碼

微信 賞一包辣條吧~

支付寶 賞一聽可樂吧~

總結

以上是生活随笔為你收集整理的php中的控制器是什么意思,理解PHP中的MVC编程之控制器_php的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。