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

歡迎訪問 生活随笔!

生活随笔

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

php

php mvc开发系列教程第三节 Controller 类实现

發布時間:2025/3/13 php 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php mvc开发系列教程第三节 Controller 类实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通過上兩節我們知道 程序通過單一入口文件的route類決定了 唯一的moudle, conttoller, action,并在最后執行了

$route->run();



? ??

/**

* 執行相應的 MCA

*

*/

private function run ()

{

$filePath = APPLICATION_PATH.'/controller/'.$this->_moudle.'/'.$this->_conttoller.'.inc.php';

$isNo = 0;

if(file_exists($filePath))

{

include "$filePath";

$controller_tp = $this->_conttoller.'Controller';

$controller = new $controller_tp;



if (method_exists($controller,$this->_action.'Action'))

{

$acion_tmp = $this->_action.'Action';

$controller->$acion_tmp();

}else

{

$isNo = 1;

}



}else

{

$isNo = 1;

}



if ($isNo)

{

$filePath = APPLICATION_PATH.'/controller/default/index.inc.php';

$this->_moudle = $this->_default['module'];

$this->_conttoller = $this->_default['conttoller'];

$this->_action = $this->_default['action'];



($this->_moudle != $this->_default['module']) && include "$filePath";

$controller = new indexController;

$controller->indexAction();

}

}

?

?

當相關'Controller'文件存在時執行

?

include "$filePath";

$controller_tp = $this->_conttoller.'Controller';

$controller = new $controller_tp;



上述三行代碼的意思是,根據確定好的 conttoller 包含相應文件,并實例化相應的conttoller。

??????

? ??

$acion_tmp = $this->_action.'Action';

$controller->$acion_tmp();

?

根據相應的Action 執行相應的action

?

?

所有的? Controller 類都集成一個公用的Controller 類,本節課我們就來分析一下公共的Controller 類

<?php

/**

* 前臺公共類 接口

* 實現公共部分代碼

*/



/**

* 本文件只能被index。php包含

*/

defined("WEB_AUTH") || die("NO_AUTH");



/**

* 包含菜單配置文件

*/



class Controller

{

public $tpl;

public $controller;

public $body;//右邊菜單

public $_route ;

public $html_;

public $tpl_;



/*

* 構造函數

*/

public function __construct()

{

$this->init();

}



/*

* 初始化變量,頂部菜單和模板

*/

protected function init()

{

global $TPL,$route;

$this->tpl = $TPL;

$this->_route = $route;

}





/**

* 模板變量傳第

*/

protected function diplayTpl()

{

$this->body || $this->body = $this->_route->getActionName();

$this->tpl->assign("body",$this->body);

/*設置本控制器的模板目錄*/

$this->controller ||$this->controller =$this->_route->getControllerName();

$this->tpl->assign("controller",$this->controller);

$this->tpl->display($this->layout);

}

/**

* smarty封裝類

* @param string $name

* @param string $value

*/

public function assign($name,$value)

{

$this->tpl->assign($name,$value);

}



/**

* 顯示另外的模板

* @param string $name

* @param string $value

*/

protected function displayOther($file)

{

$this->assign("otherTpl",TRUE);

$this->tpl->display($file);

}

/**

* 顯示某個MCA的body模板

* 0=>m 1=>c =>a

*/

protected function getMcaBody($array)

{

return 'http://www.cnblogs.com/../'.$array[0].'/body/'.$array[1].'/'.$array[2];

}

/*

* 析構函數,顯示頁面

*/

protected function __destruct()

{

$this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl();

}

/**

* 中途退出

*/

protected function _exit($msg = "")

{

$this->assign("otherTpl",TRUE);

die($msg);

}



/**

* 用 $this->html_var=value放法給變量賦值

* 用 $this->tpl_var=value放法給變量賦值

*/

protected function __set($name,$value)

{

if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_")

{

$this->assign(substr($name,5),$value);

}

}

}

?>



?

首先看

? ?

protected function __destruct()

{

$this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl();

}

?

這是所有Controller 類 生命周期結束時候要執行的函數(搜索一下php魔術方法 查看詳情)

本框架利用這時候解析模板,這樣的好處是,當Controller中相關執行完相關數據處理,后自動執行相關的模板(View);而不用每次在程序最后調用模板

?

protected function __set($name,$value)

{

if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_")

{

$this->assign(substr($name,5),$value);

}

}



這個函數簡化了程序向模板傳遞變量的方法,以smarty為例,在程序中需要執行 $tpl->assign(‘key’,$value);

來向模板中注冊變量,而此函數中簡化了此方法 ,只需 $this->html_key=$value;來實現相同的作用.(利用開發環境的提示功能,在前面聲明

public $html_;

public $tpl_;



更加簡化了向模板注冊變量

轉載于:https://www.cnblogs.com/tianliangle/archive/2012/01/07/2315585.html

總結

以上是生活随笔為你收集整理的php mvc开发系列教程第三节 Controller 类实现的全部內容,希望文章能夠幫你解決所遇到的問題。

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