首页 技术 正文
技术 2022年11月11日
0 收藏 701 点赞 2,547 浏览 7667 个字

ThinkPHP也是一个MVC框架,分视图、控制器和模型,和Yii框架相比相对较好理解,并且是轻量级的框架(相对于Yii来说),在使用Yii框架时候如果将框架放在项目文件中,用编辑器打开文件都比较慢,亲身的体会。TP代码也分工明确,便于网站的维护和管理。现在也有很多公司在用这个框架。

 一、TP的目录结构

library—————-第三方资源包

ThinkPHP

jquery

kindeditor

application————应用程序

Common—————全局公用函数

Home

Common————-局部公用函数

Conf—————配置文件

config.php

Controller———控制器

Model————–模型

View—————视图

Admin

Common————-局部公用函数

Conf—————配置文件

config.php

Controller———控制器

Model————–模型

View—————视图

Runtime————–运行时(临时文件)

public—————–公用文件

images

css

index.php

admin.php     (目录大致是这样的)

二、命名空间:虚拟的文件夹

1、定义命名空间

namespace 空间名;

namespace hello;

namespace hello\aaa\bbb;

注意:namespace必须出现在整个页面的第一行

2、导入指定空间下的类

use 空间名\类名;

use hello\Person;//导入hello空间下的Person类

use hello\aaa\Person;//导入hello\aaa下的Person类

作用:解决类的重名问题

注意:命名空间到php5.3的时候,才引入

三、入口文件

<?php
header(...);
//指定绑定的模块名
define("BIND_MODULE","模块名");
//指定应用程序目录
define("APP_PATH","application/");
//开启调试模式
define("APP_DEBUG",true);
//开启或关闭目录安全。开启后是在每个目录中生成一个空白的index.html文件
define("BUILD_DIR_SECURE",false);
//包含ThinkPHP入口文件
include_once 'library/ThinkPHP/ThinkPHP.php';
?>

注意:

1、每个控制器必须有自己的命名空间

namespace 模块名\Controller;

2、每个模型必须有自己的命名空间

namespace 模块名\Model;

3、导入一些命名空间

1)控制器与模型的父类

namespace Think;

library/ThinkPHP/Library/Think/Controller.class.php

namespace Think;

library/ThinkPHP/Library/Think/Model.class.php

2)导入指定空间下的类

use Think\Controller;

use Think\Model;

四、命名规则

.控制器的命名规则

1、必须采用大驼峰的命名规则

2、必须以Controller.class.php来结尾

IndexController.class.php

LoginController.class.php

NewsController.class.php

ReviewsController.class.php

控制器中方法的命名

1、必须采用小驼峰的命名规则

index()

add()

insert()

login()

示例:

<?php
namespace Home\Controller;
use Think\Controller; class XxxController extends Controller
{
public function 方法()
{
//逻辑代码;
$this->assign(key,value);
$this->assign(key,value);
$this->assign(key,value);
$this->display();
}
}
?>

模型的命名规则

1、必须采用大驼峰的命名规则

2、必须以表名来命名

3、必须以Model.class.php来结尾

ReviewsModel.class.php

BbsInfoModel.class.php

示例:

<?php
namespace Home\Model;//定义命名空间
use Think\Model;//导入父类
class XxxModel extends Model
{
public $tableName = "省略了前缀的表名";
public $trueTableName = "完整的表名";
}
?>

五、Think标签

          特点:

1、标签定界符:{}、<>

注意:只有输出值的标签用的是{},其他的都用的是<>

2、所有<>标签,必须都是双标签

<标签></标签>

<标签 />

3、assign方法传过来的key名,有的加$,有的不加$

注意:只有输出值、if标签加$,其他的都不加$

标签:

1、显示变量值

{$key}

2、显示数组中的某一个值

{$key.下标}

{$key[下标]}

3、if标签

<if condition=”条件”>

内容

<elseif condition=”条件”/>

内容

<elseif condition=”条件”/>

内容

<else/>

内容

</if>

4、比较标签

== eq

!=           neq

===          heq

!==          nheq

<            lt

<= elt

> gt

>=       egt

5、switch标签

<switch name=”key”>

<case value=”值1″>内容</case>

<case value=”值2″>内容</case>

<case value=”值3″>内容</case>

<default/>内容

</switch>

6、for标签

<for start=”起始值” end=”结束值” name=”循环变量名”>

内容,循环变量:{$i}

</for>

7、foreach标签

<foreach name=”key” key=”k” item=”v”>

下标:{$k}    值:{$v}

</foreach>

8、volist标签

<volist name=”key” key=”k” id=”v”>//k是序号而不是键名称

序号:{$k}   值:{$v}

</volist>

9、原样显示

<literal>

Think标签

</literal>

10、php标签

<php>

php代码;

php代码;

php代码;

</php>

11、包含html文件

<include file=”xxxx”/>

<include file=”文件夹:文件名”/>

例子:

View

Login

hello.html

<include file=”Login:hello”/>

六、ThinkPHP配置文件

1、当前模块目录的配置文件

application/Home/Conf/config.php

application/Admin/Conf/config.php

2、全局配置文件

application/Common/Conf/config.php

3、全局配置文件

library/ThinkPHP/Conf/convention.php

配置文件优先级排列顺序:

模块下的配置文件 > application/Common/Conf/config.php > tp资源包中的配置文件

建议:

1、不要修改convention.php配置文件

2、application/Common/Conf/config.php

前后、后台管理都一样的配置信息,写在这里

3、模块下的配置文件

存放的都是当前模块独立的配置信息

七、url访问方式(url路由)

1、问号传参

http://localhost/项目/index.php

http://localhost/项目/index.php?c=控制器&a=方法

http://localhost/项目/index.php?c=控制器&a=方法&名=值

———-

<a href=”index.php?c=控制器&a=方法”></a>

<img src=”public/images/xxx.jpg”>

<link href=”public/css/xxx.css”>

<script src=”library/jquery/jquery-1.4.js”>

2、pathinfo模式

http://localhost/项目/index.php

http://localhost/项目/index.php/控制器/方法.html

http://localhost/项目/index.php/控制器/方法/名/值.html

———-

<a href=”入口文件地址/控制器/方法/名/值.html”></a>

<img src=”根目录地址/public/images/xxx.jpg”>

<link  href=”根目录地址/public/css/xxx.css”>

<script src=”根目录地址/library/jquery/jquery-1.4.js”>

3.

(1)、通过传统的$_GET方法来获得

$变量 = $_GET[“参数名”];

(2)、给方法加参数

public function 方法名($参数名,$参数名…)

{}

注意:

1)方法中的参数名必须与url中的参数名相同

2)方法中的参数的个数、顺序必须与url中的参数相同

(参数的名称、个数、顺序必须全部相同)

 八、ThinkPHP系统常量

1、获得入口文件地址:链接地址、跳转地址

__APP__

2、获得根目录地址:css、image、library

__ROOT__

九、ThinkPHP页面重定向

1、跳转页面

$this->redirect(“控制器/方法”);

2、跳转页面,并url传参

$this->redirect(“控制器/方法”,array(名=>值,名=>值));

3、跳转页面,并提示信息

$this->redirect(“控制器/方法”,array(),时间,信息);

$this->redirect(“控制器/方法”,NULL,时间,信息);

 十、自定义success页面

1、在View\Public下新建一个success页面(自定义的提示信息页面)

2、控制器中通过$this->success()、$this->error()来调用

$message:提示信息、$jumpUrl:跳转地址

3、修改配置文件,设置提示信息页面的路径

‘TMPL_ACTION_ERROR’  =>  ‘Public:success’, // 默认错误跳转对应的模板文件

‘TMPL_ACTION_SUCCESS’=>  ‘Public:success’, // 默认成功跳转对应的模板文件

十一、ThinkPHP中获得模型对象的方法

1、$model = D(“模型文件名”);//必须新建模型文件

2、$model = D(“表名”);//不用新建模型文件

2、$model = M(“表名”);//不用新建模型文件

注意:表名最好是全部小写

M(“bbsInfo”)或D(“bbsInfo”)—>表名:bbs_info

十二、数据库操作

1、查询多条记录,返回:二维数组

$result = $model->select();

$result = $model->where()->select();

$result = $model->order()->select();

$result = $model->limit()->select();

$result = $model->where()->order()->limit()->select();

2、删除记录,返回:受影响的行数

$result = $model->delete();

$result = $model->where()->delete();

3、查询一条记录,返回:一维关联数组

$result = $model->find();

$result = $model->where()->find();

4、修改记录,返回:受影响的行数

$result = $model->save(一维关联数组);

$result = $model->where()->save(一维关联数组);

5、添加记录,返回:1)受影响的行数,2)新加记录的主键值

$result = $model->add(一维关联数组);

$result = $model->data(一维关联数组)->add();

6、执行sql语句,返回:二维数组

$result = M()->query(“select语句”);

7、执行insert、update、delete语句,返回:受影响的行数

$result = M()->execute(“insert|update|delete语句”);

8、count查询,返回:整数

$result = $model->count();

$result = $model->where()->count();

9、sum查询,返回:数字

$result = $model->sum(字段名);

$result = $model->where()->sum(字段名);

10、avg查询,返回:数字

$result = $model->avg(字段名);

$result = $model->where()->avg(字段名);

11、max查询,返回:数字

$result = $model->max(字段名);

$result = $model->where()->max(字段名);

12、min查询,返回:数字

$result = $model->min(字段名);

$result = $model->where()->min(字段名);

13、字段值自增

$result = $model->where()->setInc(“字段名”);//加1

$result = $model->where()->setInc(“字段名”,5);//加5

14、字段值自减

$result = $model->where()->setDec(“字段名”);//减1

$result = $model->where()->setDec(“字段名”,5);//减5

在多个页面需要同样的代码段时,可以在Controller文件夹下建立一个BaseController控制器来继承Controller父类,并且在其中添加方法来实现多个页面中相同的代码效果。在各个页面的控制器中直接继承BaseController这个类,如下代码:

 class Controller{}
class BaseController extends Controller
{
public function _initialize()
{}
} class ShowController extends BaseController
{}
class Person
{
public function __construct()
{
if(method_exists($this,_initialize))
{
$this->_initialize()
}
}
}
class Son
{
//相当于子类的构造函数
public function _initialize()
{}
}
$s = new Son();

十三、TP自带的类

ThinkPHP自带的分页类

library/ThinkPHP/Library/Think/Page.class.php

分页类的使用

use Think\Page;

$page = new Page(总记录数,每页记录数=20);

$result= $model->limit($page->firstRow,$page->listRows)->select();

$pageList = $page->show();//分页栏

           ThinkPHP自带的验证码类

library/ThinkPHP/Library/Think/Verify.class.php

1、加载验证码

1)控制器里的代码

$verify = new Verify();

$verify->length = 5;//字符个数

$verify->fontSize 20;//字符大小

$verify->entry();//显示验证码

2)视图页面显示图片

<img src=”__APP__/控制器/方法”>

2、验证码的验证

$verify = new Verify();

$result = $verify->check(用户输入的验证码);

           ThinkPHP自带的文件上传类

library/ThinkPHP/Library/Think/Upload.class.php

一种:只上传一个文件

$upload = new Upload();//实例化

$upload->maxSize = 2000000;//最大大小(以字节为单)

$upload->exts = array(“jpg”,”gif”);//允许上传的文件的扩展名

$upload->autoSub = false;//用时创建一个子目录

$upload->rootPath = “./”;//保存路径的根目录

$upload->savePath = “public/upfile/”;//保存路径

//上传文件

$myFile = $_FILES[“myFile”];

$result = $upload->uploadOne($myFile);

二种:多文件上传

$upload = new Upload();//实例化

$upload->maxSize = 2000000;//最大大小(以字节为单)

$upload->exts = array(“jpg”,”gif”);//允许上传的文件的扩展名

$upload->autoSub = false;//用时创建一个子目录

$upload->rootPath = “./”;//保存路径的根目录

$upload->savePath = “public/upfile/”;//保存路径

//上传文件

$result = $upload->upload();

注意:php.ini中与上传相关的选项

1、是否支持文件上传

file_uploads = On|Off

2、上传文件临时存储目录

;upload_tmp_dir = d:/hello

3、允许上传的最大文件

upload_max_filesize = 2M

4、同一表单最多可以上传20个文件

max_file_uploads = 20

5、post表单最大提交的数据量

post_max_size = 8M

6、最多可占用多少内存

memory_limit = 128M

已经好长时间没有用过TP框架了,大致的功能点也就这么多……

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,958
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,482
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,328
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,111
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,743
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,777