smarty建的mvc环境
================================
搭建MVC结构
================================
基于MVC,解耦合 (高内聚,低耦合),优点:易维护、易扩展
本MVC模式采用的是单一入口:
如:http://localhost/lamp45/mvc/index.php?m=stu&a=add //打开学生信息的添加界面
其中m的值stu表示访问的是StuAction a的值add表示是方法(动作)
就是访问StuAction的add方法。
1. 创建目录:
ORG 第三方扩展类
model M(模型)层目录
action A(控制)层目录
view V(视图) 层目录(smarty的模板目录)
public 公共资源目录
libs Smarty库(解压到这里即可)
view_c Smarty模板编译目录
cache Smarty静态缓存目录
configs 配置文件目录
2. 将自己写好的model类放到model目录下
model/db.class.php
3. 在ORG目录下创建一个tpl.class.php的smarty子类,用于初始化smarty(等同于以前的init.php)
代码如下:
//Smarty信息的初始化类
class Tpl extends Smarty{
public function __construct(){
parent::__construct(); //构造父类
//初始化Smarty对象中属性:
$this->template_dir = "view"; //smarty模板目录
$this->compile_dir = "view_c"; //smarty模板编译目录
$this->config_dir = "configs"; //smarty配置文件目录
$this->cache_dir = "cache"; //smarty模板静态缓存目录
//$this->caching = true; //是否开启静态缓存
//$this->cache_lifetime = 3600; //静态缓存时间(秒)
//指定定界符
$this->left_delimiter="<{"; //左定界符
$this->right_delimiter="}>"; //右定界符
}
}
4. 在action目录下创建Action类,继承Tpl类,文件名叫:action.class.php
代码如下:
//Action的控制基类
class Action extends Tpl{
public function __construct(){
parent::__construct();
}
/**
*action初始化方法(在这个方法里根据参数a的值决定调用对应的方法)
*
*/
public function init(){
//获取a参数的值
$a = isset($_GET["a"])?$_GET["a"]:"index"; //默认值为index
//判断当前Action是否存在此方法
if(method_exists($this,$a)){
//调用此方法
$this->$a();
}else{
die("没有找到{$a}对应的方法");
}
}
}
5. 在最外层,创建一个index.php的入口文件
<?php
//网站的主入口程序
//自动加载类
function __autoload($name){
$name = strtolower($name);//转成小写
if(file_exists("./action/{$name}.class.php")){
require("./action/{$name}.class.php");
}elseif(file_exists("./model/{$name}.class.php")){
require("./model/{$name}.class.php");
}elseif(file_exists("./ORG/{$name}.class.php")){
require("./ORG/{$name}.class.php");
}elseif(file_exists("./libs/".ucfirst($name).".class.php")){
require("./libs/".ucfirst($name).".class.php");
}elseif(file_exists("./libs/sysplugins/{$name}.php")){
require("./libs/sysplugins/{$name}.php");
}else{
die("错误:没有找到对应{$name}类!");
}
}
//数据连接配置文件
require("./configs/config.php");
//获取参数m的值,并创建对应的Action对象
$mod = isset($_GET['m'])?$_GET['m']:"index";
//拼装action类名
$classname = ucfirst($mod)."Action";
//创建对应的Action对象
$action = new $classname();
//执行action的初始化(action入口)
$action->init();
6. 在configs的目录下创建一个config.php的配置文件
7. 测试:
-------------------------------------------------------------
1. 在action目录下创建一个indexaction.class.php文件
/**
* 网站入口的主Action类
*/
class IndexAction extends Action{
//默认入口方法
public function index(){
$this->assign("title","MVC的测试界面");
$this->display("index.html");
}
}
2. 在view目录下创建index.html模板页面
<html>
<head>
<title><{$title}></title>
</head>
<body>
<h2><{$title}></h2>
</body>
</html>

smarty建的mvc环境的更多相关文章
- 在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数
目前项目中有个需求,需要在WebForm中去构造MVC的URL信息,这里写了一个帮助类可以在ASP.NET非MVC环境中(WebForm中)构造MVC的URL信息,主要就是借助当前Http上下文去构造 ...
- Spring MVC 环境搭建(二)
在Spring MVC 环境搭建(一)中我们知道 spring 的配置是通过 urlmapping 映射到控制器,然后通过实现Controller接口的handlerequest方法转向页面. 但这存 ...
- Spring MVC 环境搭建(一)
一.建立 JavaWeb 项目 1.建立一个 Java 项目. 2.在项目下新建一个文件夹 webapp (命名可自取,这个目录即是网站根目录),再在该文件夹下新建一个 WEB-INF 文件夹(命名固 ...
- Windows Server2008 R2 MVC 环境配置
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)
Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomc ...
- 详细的图文介绍如何利用XAMPP本地建站的环境配置教程
原文:详细的图文介绍如何利用XAMPP本地建站的环境配置教程 WordPress 是一个简便快捷,用途广,人气旺的一个开源的博客建站程序.很有很多等您去发现. 简便快捷:在性能上易于操作.易于浏览: ...
- multipath多路径实验01-构建iscsi模拟环境
multipath多路径实验01-构建iscsi模拟环境 前几天跟同事网上闲聊技术,吐槽之前自己没有配置过多路径的经历,虽然多路径的配置过程很简单,职责划分也应是主机或存储工程师来搞定,DBA只需要直 ...
- 在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数,以及如何根据URL解析出匹配到MVC路由的Controller和Action
目前项目中有个需求,需要在WebForm中去构造MVC的URL信息,这里写了一个帮助类可以在ASP.NET非MVC环境中(WebForm中)构造MVC的URL信息,主要就是借助当前Http上下文去构造 ...
- XE10 塔建 Android 开发环境 (已测试通过)
XE10 塔建 Android 开发环境 1. E:\DevCSS\Android\adt-bundle-windows-x86-20131030\adt-bundle-windows-x86-201 ...
随机推荐
- c# 动态获取http通过post传来的json数据
//获取http通过post传来的json数据 using Newtonsoft.Json; public HttpResponseMessage Query() { var request=Syst ...
- Java学习第三天之注释
编写程序时,总需要为程序添加一些注释,用以说明某段代码的作用,或者说明某个类的用途.某个方法的功能,以及该方法的参数和返回值的数据类型及意义等. 一.为什么要添加注释? (1)便于自己理解:有些人可能 ...
- vba代码阅读
#If Vba7 Then #如果是运行在64位office上 Declare PtrSafe Sub...#Else #如果是运行在32位office上 Declare Sub...#EndIf 在 ...
- python django uwsgi nginx安装
python django uwsgi nginx安装 已安装完成python/django的情况下安装 pip install uwsgi cd /usr/share/nginx/html/ vim ...
- 测试某网站的SMS验证码
to=18911121211&sms_type=sms_registration&captcha_num=9JCMw4yN5EjI6ISYoNGdwF2YiwiIw5WNwlmb3xm ...
- idou老师教你学Istio12 : Istio 实现流量镜像
微服务为我们带来了快速开发部署的优秀特性,而如何降低开发和变更的风险成为了一个问题.Istio的流量镜像,也称为影子流量,是将生产流量镜像拷贝到测试集群或者新的版本中,在引导实时流量之前进行测试,可以 ...
- ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问
报错:ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问.: 'E:\\Anoconda\\ ...
- windows下内存检测工具
1.Intel的Parallel Inspector工具,和vs集成超好, 而且还带了线程检测工具. 2.Purifyhttps://www.cnblogs.com/hehehaha/archive/ ...
- Vuex的mapGetters方法使用报错
报错信息: ERROR in ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script ...
- [唐胡璐]Selenium技巧- Highlight页面元素
大家都知道QTP的对象高亮显示功能特别强大, Selenium Webderiver也可以实现此功能。 高亮显示有时候对Debug还是相当有用的。 解决脚本: 调用脚本: 结果显示: