tp5.0中及其常用方法的一些函数方法(自己看)和技巧(不断添加中)

1.目录结构

2.路由

3..控制器

4.模型写法

5.视图标签

6.数据库操作

7.表单验证

8.分页

---------------------------

1.目录结构

project  应用部署目录
├─application           应用目录(可设置)
│  ├─common             公共模块目录(可更改)
│  ├─index              模块目录(可更改)
│  │  ├─config.php      模块配置文件
│  │  ├─common.php      模块函数文件
│  │  ├─controller      控制器目录
│  │  ├─model           模型目录
│  │  ├─view            视图目录
│  │  └─ ...            更多类库目录
│  ├─command.php        命令行工具配置文件
│  ├─common.php         应用公共(函数)文件
│  ├─config.php         应用(公共)配置文件
│  ├─database.php       数据库配置文件
│  ├─tags.php           应用行为扩展定义文件
│  └─route.php          路由配置文件
├─extend                扩展类库目录(可定义)
├─public                WEB 部署目录(对外访问目录)
│  ├─static             静态资源存放目录(css,js,image)
│  ├─index.php          应用入口文件
│  ├─router.php         快速测试文件
│  └─.htaccess          用于 apache 的重写
├─runtime               应用的运行时目录(可写,可设置)
├─vendor                第三方类库目录(Composer)
├─thinkphp              框架系统目录
│  ├─lang               语言包目录
│  ├─library            框架核心类库目录
│  │  ├─think           Think 类库包目录
│  │  └─traits          系统 Traits 目录
│  ├─tpl                系统模板目录
│  ├─.htaccess          用于 apache 的重写
│  ├─.travis.yml        CI 定义文件
│  ├─base.php           基础定义文件
│  ├─composer.json      composer 定义文件
│  ├─console.php        控制台入口文件
│  ├─convention.php     惯例配置文件
│  ├─helper.php         助手函数文件(可选)
│  ├─LICENSE.txt        授权说明文件
│  ├─phpunit.xml        单元测试配置文件
│  ├─README.md          README 文件
│  └─start.php          框架引导文件
├─build.php             自动生成定义文件(参考)
├─composer.json         composer 定义文件
├─LICENSE.txt           授权说明文件
├─README.md             README 文件
├─think                 命令行入口文件

2.路由(路由的写法 https://www.kancloud.cn/manual/thinkphp5/118029   )

Route::rule('路由表达式','路由地址','请求类型','路由参数(数组)','变量规则(数组)');

例子

Route::rule('new/:id','News/update','POST');

 

 

'test' =>'index/index/show'

加了路由之后控制器可以用url()方法获取路由

echo url('index/index/show');

也就是说,不管test怎么变,只要控制器正确,返回的就是route定义数组的别名.

 

 3.控制器

  3.1)控制器之request对象   

    public function show(Request $request)
    {
        var_dump($request);
        var_dump(\request());
        Request::instance();
    }

其中的常用方法

        //thinkphp\library\think\Request.php 基本所有方法

        $request->domain();//返回域名
        $request->pathinfo();
        $request->path();
        $request->method();
        $request->isGet();
        $request->isAjax();
        $request->get();//获取get参数 不包含pathinfo变量
        $request->param();//pathinfo值和get都传递过来
        $request->post();//post值

        session('key','value');//开启session 配置文件中开启
        $request->session();//session值
        cookie('key1','value1');
        $request->cookie();
        $request->cookie('key1');

        $request->param('key1');//页可以直接获取value1


        $request->module();//获取模块
        $request->controller();//获取控制器
        $request->action();//获取操作点

        $request->url();
        $request->baseUrl();
        $request->file();
        //input助手函数 thinkphp\helper.php
        echo $request->param('name');
        $p = 'param';
        echo $request->$p('name');
        echo input('name');//先去param,再找get post put session cookie server env path file  ...
        echo input('post.name',10);//去post值
        echo input('session.name',10);//去session值

   3.2)控制器返回值

        //更改返回类型
        //可以动态的切换控制器的返回类型 -也可以不设置
        config::Set('default_return_type','xml');//返回值是xml json 返回值自动转换
        //也可以
        $data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
    return $data;
// 指定json数据输出 return json(['data'=>$data,'code'=>1,'message'=>'操作完成']); // 指定xml数据输出 return xml(['data'=>$data,'code'=>1,'message'=>'操作完成']);

 

5.视图 (参考  https://www.kancloud.cn/manual/thinkphp5/118048   )页面引入资源文件(__STATIC__/)

https://www.kancloud.cn/manual/thinkphp5/118120

  5.1)渲染模板

namespace app\index\controller;

use think\View;

class Index 
{
    public function index()
    {
        $view = new View();
        return $view->fetch('index');
    }
}

  5.2)或者直接使用view助手函数渲染模板输出,例如:

namespace app\index\controller;

class Index 
{
    public function index()
    {
        return view('index');
    }
}

  5.3)如果继承了think\Controller类的话,可以直接调用think\Viewthink\Request类的方法,例如:

namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        // 获取包含域名的完整URL地址
        $this->assign('domain',$this->request->url(true));
        return $this->fetch('index');
    }
}

 视图实例化渲染:https://www.kancloud.cn/manual/thinkphp5/118113

   5.4)thinkphp模板中的常量 (有些已经失效可以自己定义)

<link rel="stylesheet" href="__STATIC__/layui/css/layui.css">  //<link rel="stylesheet" href="/public/static/layui/css/layui.css">
默认的模板替换规则:
../Public : 会被替换成当前项目的公共模板目录 通常是 / 项目目录 /Tpl/default/Public/
__PUBLIC__ :会被替换成当前网站的公共目录 通常是 /Public/
__TMPL__ : 会替换成项目的模板目录 通常是 / 项目目录 /Tpl/default/
__ROOT__ : 会替换成当前网站的地址(不含域名)
__APP__ : 会替换成当前项目的 URL 地址 (不含域名)
__URL__ : 会替换成当前模块的 URL 地址(不含域名)
__ACTION__ :会替换成当前操作的 URL 地址 (不含域名)
__SELF__ : 会替换成当前的页面 URL

还可以通过在项目配置文件中配置TMPL_PARSE_STRING的值来自定义替换规则,如:

TMPL_PARSE_STRING => array(
   '__PUBLIC__' => '/Common' ,  // 更改默认的 __PUBLIC__ 替换规则
   '__UPLOAD__' => '/Public/Uploads/' ,  // 增加新的上传路径替换规则
)

示例:

文件路径:/Home/Tpl/default/User/index.html,代码如下:

<p>__ROOT__代表当前网站的网址</p>
<p>__URL__代表当前模块的URL地址/index.php/User</p>
<p>../Public代表/aoli/Tpl/default/Public</p>
<p>__PUBLIC__代表项目公共文件目录/Public</p>
<p>__TMPL__代表当前项目的模板目录/aoli/Tpl/default/</p>
<p>__APP__代表当前项目的入口文件地址/index.php</p>
<p>__ACTION__代表当前的操作地址/index.php/User/index</p>
<p>__SELF__代表当前URL地址/index.php/User/</p>
<p>__UPLOAD__</p>
<form action="__URL__/add" method="post">
 <input type="text" name="username" />
 <input type="submit" value="注册" />
</form>

http://www.jb51.net/article/54217.htm

http://www.thinkphp.cn/topic/8617.html

 

 4.模型

6.数据库 ( https://www.kancloud.cn/manual/thinkphp5/118058  )

  6.1)基本使用

Db::query('select * from think_user where id=?',[8]);
Db::execute('insert into think_user (id, name) values (?, ?)',[8,'thinkphp']);

  6.2)查询构造器

Db::name('user')->where('id',1)->find();
Db::name('user')->where('status',1)->select();

使用Query对象或闭包查询

或者使用查询对象进行查询,例如:

$query = new \think\db\Query();
$query->table('think_user')->where('status',1);
Db::find($query);
Db::select($query);

或者直接使用闭包函数查询,例如:

Db::select(function($query){
    $query->table('think_user')->where('status',1);
});

添加一条数据

$data = ['foo' => 'bar', 'bar' => 'foo'];
Db::table('think_user')->insert($data);

添加多条数据

$data = [
    ['foo' => 'bar', 'bar' => 'foo'],
    ['foo' => 'bar1', 'bar' => 'foo1'],
    ['foo' => 'bar2', 'bar' => 'foo2']
];
Db::name('user')->insertAll($data);

更新数据表中的数据

Db::table('think_user')
    ->where('id', 1)
    ->update([
        'login_time'  => ['exp','now()'],
        'login_times' => ['exp','login_times+1'],
    ]);

更新某个字段的值:

Db::table('think_user')->where('id',1)->setField('name', 'thinkphp');

删除数据表中的数据

// 根据主键删除
Db::table('think_user')->delete(1);
Db::table('think_user')->delete([1,2,3]);

// 条件删除    
Db::table('think_user')->where('id',1)->delete();
Db::table('think_user')->where('id','<',10)->delete();

混合查询

$result = Db::table('think_user')->where(function ($query) {
    $query->where('id', 1)->whereor('id', 2);
})->whereOr(function ($query) {
    $query->where('name', 'like', 'think')->whereOr('name', 'like', 'thinkphp');
})->select();

查询用到的语法: https://www.kancloud.cn/manual/thinkphp5/135182

 

事务操作  

https://www.kancloud.cn/manual/thinkphp5/139063

// 启动事务
Db::startTrans();
try{
    Db::table('think_user')->find(1);
    Db::table('think_user')->delete(1);
    // 提交事务
    Db::commit();    
} catch (\Exception $e) {
    // 回滚事务
    Db::rollback();
}

 

 

7.表单验证

 

 

8.分页

 一个带条件分页

        $where = [];
        // 查询条件
        $keyword = input('keywords');
        if($keyword){
            $where['orderNumber|groupName'] = ['like',$keyword];//竖线是or的意思,可以看手册
        }
        // 查询
        $list = Db::table("reportform")
            ->where($where)
            ->order('creatTime desc')
            ->paginate(15,false, ['query' => request()->param()]);
        // 获取分页显示
        $page = $list->render();

 

 

 9.thinkphp的文件处理类

 

 

 

转:https://www.kancloud.cn/manual/thinkphp5

 

posted @ 2018-04-07 11:41  与f  阅读(3785)  评论(0编辑  收藏  举报