简单普及一下MVC [模型层M,控制层C,视图层V]
视图
视图是用户看到并与之交互的界面。
模型
模型表示企业数据和业务规则。
控制器
控制器接受用户的输入并调用模型和视图去完成用户的需求
第一个小修改
Application\Home\Controller (Controller 字面理解我们也知道这个是控制层 MVC 的C)
IndexController.class.php
修改$this->show();里的内容
还是这个文件
public function hello(){
echo ‘hello,thinkphp!’;
}
访问
http://localhost/thinkphp/Home/Index/hello
就出现了hello….
后置策略,就是在文件输出以后,继续执行。
前置对应的就是_before_hello()
public function _after_hello(){
echo "after
";
}
hello,thinkphp!after
新建业务模块
/thinkphp/目录下直接建立一个文件叫admin.php
// 检测PHP环境
if(version_compare(PHP_VERSION,"5.3.0","<")) die("require PHP > 5.3.0 !");
define("THINK_PATH","./ThinkPHP/");
// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false
define("APP_DEBUG",True);
// 定义应用目录
define("APP_PATH","./admin/");
define("APP_NAME","admin");
// 引入ThinkPHP入口文件
require "./ThinkPHP/ThinkPHP.php";
// 亲^_^ 后面不需要任何代码了 就是如此简单
程序将自动创建admin文件夹,并存放所有需要的文件目录
进入admin的控制模块找到文件 IndexController.class.php
admin\Home\Controller
$this->show("
:)
欢迎使用 ThinkPHP!
[ 您现在访问的是Home模块的Index控制器 ]<script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>","utf-8");
屏蔽以上信息
加上一句
$this->display();
\thinkphp\admin\Home\View\Index (View 视图层)
这里目录下 搞一个index.html
随便写点
就好了。
模板赋值
IndexController.class.php里
$this->assign(‘name’,"月小升’);
$this->display();
\thinkphp\admin\Home\View\Index\index.html
我是:{$name}
就能显示了。
学一点数据库的配置
thinkphp\Application\Home\Conf
下的文件config.php
return array(
//"配置项"=>"配置值"
// 添加数据库配置信息
"DB_TYPE" => "mysql", // 数据库类型
"DB_HOST" => "localhost", // 服务器地址
"DB_NAME" => "thinkphp", // 数据库名
"DB_USER" => "root", // 用户名
"DB_PWD" => "", // 密码
"DB_PORT" => 3306, // 端口
"DB_PREFIX" => "think_", // 数据库表前缀
);
public function hello(){
echo "hello,thinkphp!";
}
改成
public function hello(){
echo "hello,thinkphp!";
$Data = M("User"); // 实例化Data数据模型
$this->data = $Data->select();
//$this->display();
print_r($this->data);
}
数据库建立一个表think_user
执行http://localhost/thinkphp/Home/Index/hello
hello,thinkphp!Array ( [0] => Array ( [id] => 1 [name] => ghj [pwd] => 123456 ) ) after
读取原生SQL
echo M("User")->getLastSql();
执行原生SQL
使用原生SQL很简单,我们甚至不需要实例化任何的模型,例如:
$Model = new Model(); // 实例化一个空模型
下面的方法是等效的
$Model = D(); 或者 $Model = M();
// 下面执行原生SQL操作
$Model->query("select * from think_user where status=1");
$Model->execute("update think_user set status=1 where id=1");
如果你实例化了某个模型,仍然可以执行原生SQL操作,不受影响,例如:
$User = D("User");
$User->query("select * from think_user where status=1");
$User->execute("update think_user set status=1 where id=1");
http://m.bbyears.com/jiaocheng/63911.html
推荐访问:thinkphp框架原理