thinkphp 开发
TP 框架: 1.MVC 设计模式 2.模板引擎 3.常用操作类
MVC 设计模式: M model 数据模型 V view 视图 C control 控制器
控制器lib路由访问,一般http://xxx.com/index.php/index/test
1 2 3 public function test(){ echo "aaaaaa"; }
渲染模版视图view一般默认渲染tpl下的视图文件
1 2 3 public function test(){ $this->display(); }
值传递渲染模版
1 2 3 4 public function test(){ $this->assign("name","User1"); $this->display(); }
网站值传递
1 2 3 4 5 public function test(){ echo $_GET["id"]; $this->assign("name","User1"); $this->display(); }
建议以第一个做值传递http://www.xx.com/index.php/index/test/id/123
/index.php/index/test?id=123
全局常量渲染
1 2 3 4 5 6 7 8 <p>../Public</p> <p>__TMPL__</p> <p>__PUBLIC__</p> <p>__ROOT__</p> <p>__APP__</p> <p>__URL__</p> <p>__ACTION__</p> <p>__SELF__</p>
配置默认控制器模块
1 2 3 4 5 6 <?php return array( //'配置项'=>'配置值' 'DEFAULT_MODULE'=>'User', ); ?>
common.php
1 2 3 4 5 <?php function show(){ echo "show aa"; } ?>
1 2 3 4 5 6 7 public function test(){ // echo __ROOT__.'/Public/Images/sb.jpeg';; // echo $_GET["id"]; show(); $this->assign("name","User1"); $this->display(); }
权限验证
1 2 3 4 5 6 7 <?php class CommonAction extends Action { function _initialize(){ echo "进行权限验证!"; } }
继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends CommonAction { public function index(){ $user=M('User'); $this->rows=$user->select(); $this->display(); } public function del(){ $id=$_GET['id']; $user=M('User'); if($user->delete($id)){ $this->success('删除成功',U('index')); } } public function show(){ echo "<h1>aaaaaaaaaaa</h1>"; } public function test(){ // echo __ROOT__.'/Public/Images/sb.jpeg'; // echo $_GET["id"]; show(); $this->assign("name","User1"); $this->display(); } }
1 2 3 4 控制左右定界符: 'TMPL_L_DELIM' => '<{', 'TMPL_R_DELIM' => '}>',
获取路由访问
输出控制路由器
url重定向
1 $this->redirect('','',5,'页面跳转');
1 2 3 4 5 6 7 8 9 地址跳转: 1.正确 本模块: $this->success('ok','show'); 跨模块: $this->success('添加成功',U('User/index')); 2.错误 本模块: $this->error('error','show');
自定义模块
插入
1 2 3 4 5 6 7 8 public function sqlselecgt(){ echo "insert sql"; $arr['id'] = '2'; $arr['username'] = 'user3'; $arr['password'] = '4566'; $user = M("user"); $user->add($arr); }
映射和验证逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 protected $_auto=array( array('password','md5','3','function'), ); protected $_validate=array( array('username','require','用户名不能为空'), array('username','checkUsername','用户名长度不正确',2,'callback'), array('password','require','密码不能为空'), array('codes','require','验证码不能为空'), array('codes','checkCode','验证码有误',2,'callback'), #array('fcode','Code','验证码有误',2,'callback'), ); function checkCode($codes){ if($codes!=$_SESSION['scode']){ return false; } } function checkUsername($username){ if(strlen($username)<6){ return false; } } protected $patchValidate = true;
CURD操作
1 2 3 4 5 6 create $arr['username'] = '12312312451'; $arr['password'] = 'abc123'; $user = D("User"); $user->create($arr); $user->add()
1 2 3 4 delete $user = M("User"); $id = 1; $user->delete($id);
1 2 3 4 5 6 7 8 9 10 11 select $user = M("User"); $row = $user->select(); echo "<pre>"; print_r($row); echo "</pre>"; $row = $user->where(array("id"=>$id))->select(); echo "<pre>"; print_r($row); echo "</pre>";
1 2 3 4 5 find $row = $user->where(array("id"=>$id))->find(); echo "<pre>"; print_r($row); echo "</pre>";
1 2 3 4 5 6 7 update $user = M("User"); $arr["id"] = 123123123; $arr["username"] = "myself"; $arr["password"] = "admin123123"; $user -> create(); $user -> save();
过滤重复值查询
1 2 3 4 5 $user = M("User"); $user -> Distinct(true)->field('username')->select(); echo $user->getLastSql() SELECT DISTINCT `username` FROM `user`
模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class UserModel extends Model{ protected $_map =array( 'id' => 'id', 'name' => 'username', 'pass' => 'password', ); protected $_auto=array( array('password','md5','3','function'), ); protected $_validate=array( array('username','require','用户名不能为空'), array('username','checkUsername','用户名长度不正确',2,'callback'), array('password','require','密码不能为空'), array('codes','require','验证码不能为空'), array('codes','checkCode','验证码有误',2,'callback'), #array('fcode','Code','验证码有误',2,'callback'), ); function checkCode($codes){ if($codes!=$_SESSION['scode']){ return false; } } function checkUsername($username){ if(strlen($username)<6){ return false; } } protected $patchValidate = true; }
模版引用赋值
1 2 3 4 5 6 7 8 9 10 11 12 <!doctype html> <html> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <h1>Index/index</h1> <h1><{$a+$b}></h1> </body> </html>
1 2 3 4 5 6 7 8 9 public function index(){ // $user=M('User'); // $this->rows=$user->select(); // $this->display(); $this->a = 10; $this->b = 20; $this->display(); }
for循环
1 2 3 <for start="1" end="10"> <h1><{$i}></h1> </for>
判断
1 2 3 <if condition='$i==2'> <h1><{$i}></h1> </if>
循环
1 2 3 <foreach name='rows' item='val'> <h1><{$val.username}></h1> </foreach>
登陆
1 2 3 4 5 6 7 8 9 10 $user = M('User'); $_POST['password'] = $_POST['password']; $row = $user->where($_POST)->find(); if($row){ session('username',$_POST['username']); session('login',1); $this->success('登陆成功','login'); }else{ $this->error('登陆错误','login'); }