仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1663|回复: 18
打印 上一主题 下一主题

[学习教程] PHP教程之MVC with PHP(二)

[复制链接]
透明 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:29:46 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
培训的第四阶段,就是应用PHP语言开发实际的程序。以结合实际的项目开发来进行学习,效果真的很好,在学习完之后就开始练习,能比较容易掌握所学的知识,这是学校的学习所没法比的。   MVC with PHP(一)中的bug的成绩是存在,最大的成绩是日记体系的成绩,等完成这这个引见后我后把全体更正的法式源码打包
出来,这里就临时不做更改了.
先来看看在application.class.php中是若何创立controller实例的:

PHP代码:--------------------------------------------------------------------------------
/**
* 履行函数
*
* 此类独一对外的一个接口
**/
public function run()
{
$this->parsePath();
$this->checkSecurity($this->module, $this->action);
1. $controller = new $this->controllerClassName();
2. $controller->{$this->action}();
$this->writeLog($this->module, $this->action);
}

--------------------------------------------------------------------------------

Application这个类在实例后独一可停止挪用的一个函数,它依据用户的URL恳求来剖析得出所需求的Controller类名,然后实例化这个类(下面标1的中央),再挪用从URL中获得的举措称号(下面标2的中央),

这个举一个复杂的例子:
URL: http://localhost/?module=news&action=showList
Application经由过程剖析这个URL重到controllerClassName=news, action=showList,然后它将在包括处置这个controller类的文件名(在Application->getControllerFile()中停止),然后实例化News这个
controller类(标1的中央), 随后挪用它的举措showList(标2的中央).
来看看newsController.php中的内容:
=============================================================

PHP代码:--------------------------------------------------------------------------------

<?php
/**
* FileName: newsController.php
* Introduce: 旧事掌握类
*
* @author: 巨匠兄
* @Email: teacherli@163.com
* @version $Id$
* @copyright 2004-10-26
**/
include_once ("./controller/comm/controller.class.php");
include_once ("./model/news/newsModel.php");

class NewsController extends Controller
{
private $model;

/**
* 机关函数
*
**/
public function __construct()
{
parent::__construct();
$this->model = new NewsModel();
$this->setSmartyTemplate_dir("./view/news");
}

/**
* 显示旧事列表
*
**/
public function showList()
{
1. $newsList = & $this->model->getList();

2. $this->smarty->assign("newsList", $newsList);
3. unset($newsList);

4. $this->smarty->display("newsList.html");
}
}
?>

--------------------------------------------------------------------------------

==============================================================
起首,NewsController类承继自公共类Controller,在类停止初始化时发生一个NewsModel类,这个类是一个model类,由这个类担任旧事模块一切的对数据库的交互. parent::__construct()挪用父类的机关函数,完成对view的掌握类Smarty的初始化.$this->setSmartyTemplate_dir("./view/news")将模板目次定位在./view/news目次.

然后看咱们下面的例子,恳求URL为http://localhost/?module=news&actio...List,暗示要挪用
showList这个举措,看NewsController类的showList()成员函数:
1. $newsList = & $this->model->getList(): $this->model在NewsController初始化时创立,这一句要利用$this->model从数据库里提掏出一个旧事列表,这个列表固然就是Smarty在操作轮回块时需求的二维数组了,在NewsModel类中,它是采取ADODB回传的一个二维数组.
2. $this->smarty->assign("newsList", $newsList): 熟习吧,smarty中轮回块的法式掌握
3. unset($newsList):思索到效力成绩,关于这些一时变量在利用完成后即时将它unset。
4. $this->smarty->display("newsList.html"):利用smarty来显示view.

人人看分明了吗?实践上controller类要做的工作就是如许:1.挪用model从数据库掏出纪录 2.操

作Smarty显示view。
再来看看NewsController的父类Controller类的源码:
===========================================================

PHP代码:--------------------------------------------------------------------------------

<?php
/**
* FileName: controller.class.php
* Introduce: Base class of controller
*
* @author: 李晓军
* @Email: teacherli@163.com
* @version $Id$
* @copyright 2004-10-26
**/

include_once ("./comm/smarty/Smarty.class.php");
include_once ("./comm/config.inc.php");

abstract class Controller
{
private $smarty;

/**
* 体系构建函数
* 初始化Smarty
**/
function __construct()
{
$this ->smarty = new Smarty();

$this->smarty->template_dir = "./view/templates";
$this->smarty->compile_dir = "./view/templates_c";
$this->smarty->cache_dir = "./view/cache";
$this->smarty->cache_lifetime = 60 * 60 * 24;
$this->smarty->caching = false;
$this->smarty->left_delimiter = "<{";
$this->smarty->right_delimiter = "}>";
}

/**
* 设置smarty模板途径
*
* @param string $template
**/
public function setSmartyTemplate_dir($template)
{
$this->smarty->template_dir = $template;
}

/**
* 设置smarty是不是停止缓存
*
* @param boolean $cache
**/
public function setSmartyCache($cache = false)
{
$this->smarty->cache = $cache;
}

/**
* 设置smarty缓存工夫
*
* @param string $cacheLifetime
**/
public function setSmartyCacheTime($cacheLifetime)
{
$this->smarty->cache_lifetime = $cacheLifetime;
}

/**
* 举措被履行后一个长久的提醒
*
* @param string $module 从头定向到的模块称号
* @param string $action 从头定向的举措称号
* @param string $params 参数称号
* @param string $message 提醒信息
**/
public function redirect($module, $action, $params="", $message="举措已被胜利执

行")
{
$time = WAIT_FOR_TIME;
$params = ("" == $params) ? "" : "&$params";
$URL = "?module=" . $module . "&action=" . $action . $params;

//从头定Smarty模板目次大公用目次
$this->setSmartyTemplate_dir("./view/templates");
$this->smarty->assign("URL", $URL); //重定向的目次
$this->smarty->assign("message", $message); //提醒信息
$this->smarty->assign("time", $time);
$this->smarty->display("wait.html");
}

/**
* 挪用本类不存在的办法时停止的处置
*
* @param string $name
* @param string $parameter
**/
public function __call($name, $parameter)
{
throw new ActionNotAllowException("对不起,你所恳求的举措 <b>$name</b> 没有界说

...<br>");
}

/**
* 析构函数
*
**/
public function __destruct()
{
unset($this->smarty);
}
}
?>

--------------------------------------------------------------------------------

==============================================
Controller是一个笼统类,也就是说它不成以直接利用new 来发生一个实例对象,在类的机关函数里发生一个Smarty类,并对其停止根基的设置。其它的几个函数是对Smarty对象停止设置的成员函数, 这里来看看这两个函数:

public function redirect($module, $action, $params="", $message="举措已被胜利履行"):
这是一个从头定向成员函数,它的感化是当咱们对模块停止一些操作后给出的提醒页面,然后经由设置好的工夫主动从头定向到另外一个地位,例如咱们要对旧事停止一些删除,删除胜利后咱们要给用户前往如许一个页面,告知用户操作已胜利,请待n秒后主动前往....这在服装论坛中是很罕见的,这里我也援用了如许的战略。

public function __call($name, $parameter):
当类挪用类没有声明的函数时利用这个函数停止处置,这可是个好东东,有了它,可使用对法式

的掌握加倍复杂了,人人可以尝尝这个办法....

好了,Controller 局部就谈到这里了。

  在学习HTML中我想边学边做是最有效的方式,当然这一方式对于学习PHP同样是最有效的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-20 20:25

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表