发布自己写的一个弱智 PHP 框架 Fri, Feb 2. 2007
最近给学校做一个院系的网站,很俗的,就是一个很简单的后台管理,前台页面的系统。后台主要功能就是新闻的增删改、页面的增删改、教授/教师信息的增删改以及用户的管理。考虑到这种系统可能对于小的业务来说比较常见,就是说分成几个模块,每个模块对应着一些动作的系统,所以自己写了一个简单的弱智框架,便于以后的开发。
对本系统进行功能分解,主要是新闻、页面、教授/教师信息、栏目、用户模块,每个模块基本上是增删改操作,有的模块有一些特别的操作。公共模块主要是登陆和权限验证。
Root ├─class ├─images ├─include ├─library │ └─smarty ├─module │ ├─category │ ├─censor │ ├─common │ ├─page │ ├─post │ ├─professor │ └─user ├─template │ ├─cache │ ├─compile │ ├─config │ ├─module │ │ ├─category │ │ ├─common │ │ ├─page │ │ ├─post │ │ ├─professor │ │ └─user │ └─nugget ├─xinha └─yui
系统结构如下:
- index.php,主程序入口,所有程序都从 index.php 进入,便于以后增加对友好 url 等特性的支持;
- class 文件夹放所有自己写的类库;
- include 文件夹里面是一些需要 include 进来的东西,比如数据库常用函数等;
- library 文件夹里面是第三方类库,这个系统比较小,就只有 Smarty 模版系统;
- module 文件夹为所有模块的 PHP 文件,
- 每个模块对应着一个文件夹,例如新闻模块对应 post 文件夹;
- 每个模块的一个动作着一个同名文件,例如显示新增新闻页面,对应 insert.php 文件,插入新闻则对应着 insert.do.php 文件;
- 每个模块文件夹下面如果有 common.php、preproc.php、postproc.php 文件则特别处理,参见 index.php 文件;
- 每个模块文件夹下面其他的文件则可以自己设定,比如一个模块公共表单验证等;
- template 文件夹,Smarty 的模版文件,
- cache、compile、config,都是 Smarty 要求的,呵呵;
- module 子文件夹,和上面的 module 文件夹差不多,也是一个模块对应一个文件夹,一个页面对应一个模版文件。
- nugget 子文件夹,放一些 HTML nugget 了,比如 header、title、footer、menu 这些;
- 根文件夹下面还有 images、styles 等不需要特别解析的文件夹,注:yui文件夹里面是 Yahoo! UI Library 的发布版,很好的东东,推荐使用 :-),xinha 以前已经介绍过了。
index.php 的内容:
- <?php
- require_once "config.php";
- require_once $ABSOLUTE_PATH . "common.php";
- $pageModule = new PageModule();
- // get module and action value
- if (!$pageModule->isLogin() && ($module != "common" && $action != "login.do" )) {
- $module = "common";
- $action = "login";
- } else {
- if ($module == null) {
- $module = "common";
- }
- }
- // load module common pre proccess code
- $filename = $ABSOLUTE_PATH . "admin/module/" . $module . "/preproc.php";
- require_once $filename;
- }
- // load module action specific code
- $filename = $ABSOLUTE_PATH . "admin/module/" . $module . "/" . $action . ".php";
- require_once $filename;
- }
- // load module common post proccess code
- $filename = $ABSOLUTE_PATH . "admin/module/" . $module . "/postproc.php";
- require_once $filename;
- }
- $tpl_module = $module;
- $tpl_action = $action;
- $tpl_msg = $pageModule->getMessage();
- $tpl_pageModule = $pageModule;
- // show smarty template
- if ($pageModule->getTplFile()) {
- $pageModule->display();
- } else {
- $tplFile = "module/" . $module . "/" . $action . ".html";
- $pageModule->setTplFile($tplFile);
- $pageModule->display();
- }
- ?>
pageModule.php 文件,所有的页面逻辑都在这里面:
- <?php
- class PageModule {
- /**
- User information
- *
- @var User
- */
- private $user = null;
- /**
- Template class
- *
- @var Template
- */
- private $tpl = null;
- /**
- Page message
- *
- @var Array()
- */
- /**
- Setup user and tempalte
- */
- public function __construct() {
- $this->user = $_SESSION["user"];
- } else {
- $this->user = new User();
- }
- $this->tpl = new Template();
- }
- /**
- Set user to this object and session
- *
- @param User $argUser User information
- */
- public function setUser($argUser) {
- $this->user = $argUser;
- $_SESSION["user"] = $this->user;
- }
- /**
- Get session user
- @return User user User information
- */
- public function getUser() {
- return $this->user;
- }
- /**
- Get proccessed request array
- *
- @param array $argHtml An array of variables that DON'T need to be convert all applicable characters to HTML entities
- @param boolean $argTrim Whether need to be trimed, default is true
- */
- public function getRequest($argHtml = null, $argTrim = true) {
- foreach($_REQUEST as $key => $value) {
- $request[$key] = $value;
- } else {
- if ($argTrim) {
- }
- if ($argHtml) {
- // do nothing
- } else {
- // TODO:
- //$value = htmlentities($value);
- }
- }
- $request[$key] = $value;
- }
- }
- return $request;
- }
- /**
- Is login...
- *
- @return boolean User login status
- */
- public function isLogin() {
- if ($this->user == null) {
- return false;
- } else {
- return $this->user->isValid();
- }
- }
- /**
- Template var setter
- *
- @param String $argTplFile Templater file name
- */
- public function setTplFile($argTplFile) {
- $this->tpl->setTplFile($argTplFile);
- }
- /**
- Template var getter
- *
- @return String Templater file name
- */
- public function getTplFile() {
- return $this->tpl->getTplFile();
- }
- /**
- Redirect
- *
- @param unknown_type $argPage Redirect URL
- */
- public function redirect($argPage) {
- if ($this->message) {
- $_SESSION["message"] = $this->message;
- }
- }
- /**
- Display template
- *
- @param boolean $isContinue Whether to continut this programe after template shown
- */
- public function display($isContinue = false) {
- global $tpl_msg;
- }
- $tpl_msg = $this->getMessage();
- $this->tpl->display($isContinue);
- }
- /**
- */
- public function getRedirect() {
- $redirect_url = $_SERVER["REDIRECT_URL"];
- $request_uri = $_SERVER["REQUEST_URI"];
- return $redirect_url;
- } else {
- return "index.php";
- }
- }
- /**
- Add a message to message pool
- *
- @param String $argType Message type, info, warning, error
- @param unknown_type $argText Message text
- @param unknown_type $argLink Any links
- */
- "type" => $argType,
- "text" => $argText,
- "link" => $argLink
- );
- }
- /**
- Message getter
- */
- public function getMessage() {
- return $this->message;
- }
- /**
- The same as assign() in Smarty
- */
- public function tplAssign($argName, $argVars) {
- $this->tpl->assign($argName, $argVars);
- }
- }
- ?>
其他的,pageModule 里面定义了一个 message 变量,可以向页面传递要提示的信息;Template 类,对 Smarty 类的简单继承,省得每次都设置 Smarty 的参数。
而且借鉴了 dotProject 中的 DpObject 类,所有的模块的类都继承自这个类,嗯,添加删除什么的就很简单了,只要 $post->store() 或者 $post->delete() 就行了,hoho。
Trackbacks
Trackback specific URI for this entry
No Trackbacks
