首页 > 作文

thinkphp5基础

更新时间:2023-04-08 03:10:33 阅读: 评论:0

【thinkphp5框架的目录结构,以及使用框架model 、controler、view的使用,以及错误调试和日志记录

thinkphp5 在php5.5版本以上”no input file specified“问题解决:

  public/.htaccess文件中的

  rewriterule ^(.*)$ index.php/$1 [qsa,pt,l]

  在默认情况下会导致no input file specified.

  修改成

  rewriterule ^(.*)$ index.php [l,e=path_info:$1]

  问题解决

配置文件的使用

application/index.controller/demo.php

<?phpnamespace app\index\controller;u think\config;class demo extends ba{    // 配置文件的使用    public function config() {        // 默认一页显示15条        $config = config::get("paginate");        dump($config);//15        $config = config("paginate.type");        dump(config("?paginate"));//boolean true        $config = config("paginate.type");        dump(config("?paginate111"));//boolean fal        dump(config::has("paginate"));//boolean true        dump(config("cyy"));        // array (size=1)          // cyy' => int 1          dump(config("redis.host"));//string '127.0.0.1'    }}

application/extra/redis.php

<?php// 配置文件的使用return [    "host" => "127.0.0.1",];

application/index/config/php

<?php// 配置文件的使用return [    "cyy" => [        'cyy' => 1,            ]];

路由的使用

自定义路由

application/index/controller/video.php

<?phpnamespace app\index\controller;class video extends controller {    // 自定义路由    public function getvideo() {        $id = input("param.id");        // /d/file/titlepic/getvideo        //  => 域名/video/2        dump($id);    }}

application/route.php

<?php// +----------------------------------------------------------------------// | thinkphp [ we can do it just think ]// +----------------------------------------------------------------------// | copyright (c) 2006~2016 /d/file/titlepic/www.thinkphp.cn all rights rerved.// +----------------------------------------------------------------------// | licend ( http://www.apache.org/licens/licen-2.0 )// +----------------------------------------------------------------------// | author: liu21st <liu21st@gmail.com>// +----------------------------------------------------------------------u think\route;// 自定义路由 域名/video/2// route::rule("video/:id", "index/video/getvideo");//route::get("video/:id", "index/video/getvideo");// 定义get请求路由//route::post("video/:id", "index/video/getvideo");// 定义post请求路由// 组合写法route::get([    'video/:id' => ["index/video/getvideo", [], ['id' => '\d+']],]);

控制器的使用

application/index/controller/demo.php

<?phpnamespace app\index\controller;u think\config;u think\request;class demo extends ba{    /**     * 初始化     * @auth   cyy     * @return [type] [description]     */    public function _initialize() {        dump("这是initialize");    }    public function test() {        // 数组转json格式返回        return json(["as" => 1, "cyy" => "test"]);    }    public function hello() {        var_dump(input("param."));        return "index-index-hello";    }    // 控制器-跳转    public function abc() {        $id = input("param.id", 0, "intval");        if($id == 1) {            $this->success("操作成功", "admin/index/index");        }elif($id == 2) {            $this->error("操作失败");        }        }    // 控制器-重定向    public function ef() {        $this->redirect("hello", ["id" => 1, "ms" => 123]);        //redirect("https://baidu.com");    }    // 控制器-请求    public function requestdata() {        $request =  request::instance();        //访问/d/file/titlepic/requestdata        dump(request()->ispost());//boolean fal        dump(input("?get.ids"));//boolean true        dump(request()->has("ids", "get"));//boolean true    }}

application/index/controller/ba.php

<?phpnamespace app\index\controller;u think\controller;class ba extends controller {    /**     * 空操作     * @auth   singwa     * @param  [type] $name [description]     * @return [type]       [description]     */    // 控制器-空操作    public func全国一卷数学答案tion _empty($name) {        // todo        return $name;    }}

数据库配置与model层数据操作

application/databa.php

<?php// +----------------------------------------------------------------------// | thinkphp [ we can do it just think ]// +----------------------------------------------------------------------// | copyright (c) 2006~2016 /d/file/titlepic/www.thinkphp.cn all rights rerved.// +----------------------------------------------------------------------// | licend ( http://www.apache.org/licens/licen-2.0 )// +----------------------------------------------------------------------// | author: liu21st <liu21st@gmail.com>// +----------------------------------------------------------------------return [    // 数据库类型    'type'            => 'mysql',    // 服务器地址    'hostname'        => 'localhost',    // 数据库名    'databa'        => 'test',    // 用户名    'urname'        => 'root',    // 密码    'password'        => '123456',    // 端口    'hostport'        => '3306',    // 连接dsn    'dsn'             => '',    // 数据库连接参数    'params'          => [],    // 数据库编码默认采用utf8    'chart'         => 'utf8',    // 数据库表前缀    'prefix'          => 'test_',    // 数据库调试模式    'debug'           => true,    // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)    'deploy'          => 0,    // 数据库读写是否分离 主从式有效    'rw_parate'     => fal,    // 读写分离后 主服务器数量    'master_num'      => 1,    // 指定从服务器序号    'slave_no'        => '',    // 是否严格检查字段是否存在    'fields_strict'   => true,    // 数据集返回类型    'resultt_type'  => 'array',    // 自动写入时间戳字段    'auto_timestamp'  => fal,    // 时间字段取出后的默认时间格式       'datetime_format'  => fal,    // 是否需要进行sql性能分析    'sql_explain'     => fal,];

模型
application/common/model/ba.php

<?phpnamespace app\common\model;u think\model;class ba extends model{    protected $autowritetimestamp = true;    //protected $createtime = 'create_a_time';    /**     * 新增逻辑     * @auth  singwa     * @param array  $data [description]     * @return  int     */    public function add($data = []) {        if(empty($data) || !is_array($data)) {            return fal;        }        $this->allowfield(true)->save($data);        return $this->id;    }}

application/common/model/video.php

<?phpnamespace app\common\model;class video extends ba{    /**     * 定义一个关联 1-1     * @auth   singwa     * @return [type] [description]     */    public function videofile() {        return $this->hasone("videofile"); // video_id    }}

application/common/model/videofile.php

<?phpnamespace app\common\model;class videofile extends ba{    }

application/index/controller/video.php

<?phpnamespace app\index\controller;u think\db;u app\common\model\video as videomodel;class video extends ba {    // 数据库query查询    public function mysql() {        $video = db::query("lect * from test_video where id=2");        dump($video);    }    // 模型的使用    public function model() {        // 获取id为2的数据        $video = videomodel::get(2);        //dump($video);        $video = new videomodel();        $video->title = "cyy-test";        $video->description = "cyy-test-description1";        //dump($video->save());//int 1 插入数据保存成功        $data = [            "title" => "cyy-test3",        ];        //dump($video->save($data));//dump($video->save());//int 1 插入数据保存成功    }    // 增    public function add() {        $video = model("video");        $data = [            "title" => "cyy-test6",            "mpt" => 1,        ];                $id = $video->add($data);        dump($id);//string '6' (length=1)    }    // 查    public function lect() {        $conditon = [            "status" => 1,        ];        //$videos = model("video")->where($conditon)->lect();        $videos = model("video")            ->where($conditon)            ->limit(1)            ->order("id", "desc")            ->lect();        dump($videos);    }    // 改    public function update() {        $updatadata = [            "title" => "cyy你好鸭"        ];        $wherecondition = [            "id" => 1,        ];        //$res = model("video")->allowfield(true)->save($updatadata, $wherecondition);        //echo model("video")->getlastsql(); //作用非常重要        //dump($res);        model("video")->where($wherecondition)            ->update($updatadata);        echo model("video")->getlastsql();//update `test_video` t `title`='cyy你好鸭' where `id` = 1        }    // 删    public function delete() {        // 这种场景是 真正的删除        /*$res = model("video")->where("id", ">", 18)->delete();        echo model("video")->getlastsql();        dump($res);*/        // 在实际工作当中 我们的删除一般不直接删除, 所以一般假删除        //  修改status => -1        model("video")->where(["id" => 6])            ->update(["status" => -1]);        echo model("video")->getlastsql();//update `test_video` t `status`=-1 where `id` = 6    }//一对一关联    public function correlation() {        $video = model("video")->find(1);        //halt($video->videofile->file);            //dump(videomodel::haswhere("videofile", ["video_id" => 1])->find());//string 'file1' (length=5)                // 1vs1 插入        model("video")->title = "1vs1-add-test";        model("video")->image = "1vs1.gif";        model("videofile")->file = "1vs1.flv";        model("videofile")->status = 1;         model("video")->videofile = model("videofile");         //dump(model("video")->together("videofile")->save());         // 1vs1 更新操作         $video = model("video")->find(1);         $video->title = "1vs1-update-test";         $video->videofile->file = "1vs1-update.mp4";         //dump($video->together("videofile")->save());         // 1 vs n  查询         //dump(model("video")->videofile()->where("video_id", 1)->lect());         //dump(videomodel::haswhere('videofile', ["video_id" => 1])->lect());        }}

视图层

application/index/controller/video.php

<?phpnamespace app\index\controller;u think\db;u app\common\model\video as videomodel;class video extends ba {    public function demo() {        $video = model("video")->where(["id" => 1])->find();        $videos = model("video")->where("id", ">", 1)->lect();                // halt($video->update_time);//halt=dump+exit        // halt($video->update_time);        // halt($video->toarray());        return $this->fetch("", [            "name" => "cyy",            "names" => ["name" => "hello , cyy!"],            "video" => $video,            "videos" => $videos,            "id" => input("param.id")        ]);    }}

application/index/view/video/demo.html

<html><body>    {$name}<br />    {$names['name']}<br />    {$names.name}<br />    {$video->title}<br />    {$video.title}<br />    {$video:title}<br />  1850年  {eq name="name", value="cyy1"}        cyy您好1111<br />    {el /}        不是cyy<br />    {/eq}    {$request.get.id}<br />    {$video->create_time|date="y-m-d h", ###}<br/>    {$video->status|status}<br/>    {volist name="videos" id="vo"}        {$vo.id} --------  {$vo.title} <br / >    {/volist}</body></html>

日志定位

application/index/controller/demo.php

<?phpnamespace app\index\controller;u think\config;u think\request;u think\log;// 日志定位class demo extends ba{    public function logtest() {        log::write("testlog".json_encode(input("param.")));        return 1;    }}

runtime/log下的当天文件夹里的.log文件

[ 2020-01-17t13:02:47+08:00 ] 127.0.0.1 127.0.0.1 get /index/demo/logtest?mst=1&mp=45[ log ] phptest.com/index/demo/logtest?mst=1&mp=45 [运行时间:0.022899s][吞吐率:43.67req/s] [内存消耗:1,372.95kb] [文件加载:33][ log ] testlog{"mst":"1","mp":"45"}[ 2020-01-17t13:02:47+08:00 ] 127.0.0.1 127.0.0.1 get /index/demo/logtest?mst=1&mp=45[ log ] phptest.com/index/demo/logtest?mst=1&mp=45 [运行时间:0.233506s][吞吐率:4.28req/s] [内存消耗:1,571.42kb] [文件加载:40][ info ] [ lang ] d:\phpstudy_pro\www\phptest\thinkphp\lang\zh-cn.php[ info ] [ route ] array (  'type' => 'module',  'module' =>   array (    0 => 'index',    1 => 'demo',    2 => 'logtest',  ),)[ info ] [ header ] array (  'cookie' => 'thinkphp_show_page_trace=0|0; pgv_pvi=98915328; pgv_si=s6064574464; thinkphp_show_page_trace=0|0',  'accept-language' => 'zh-cn,zh;q=0.9,en-us;q=0.8,en;q=0.7',  'accept-encoding' => 'gzip, deflate',  'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',  'ur-agent' => 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, li最伤感的名字ke gecko) chrome/77.0.3865.75 safari/537.36',  'upgrade-incure-requests' => '1',  'cache-control' => 'no-cache',  'pragma' => 'no-cache',  'connection' => 'clo',  'host' => 'phptest.com',)[ info ] [ param ] array (  'mst' => '1',  'mp' => '45',)[ info ] [ run ] app\index\controller\demo->logtest[ d:\phpstudy_pro\www\phptest\application\index\controller\demo.php ][ info ] [ log ] init file

trace调试

application/config.php

// 应用调试模式    'app_debug'              => true,    // 应用trace    'app_trace'              => true,

application/index/controller/demo.php

<?phpnamespace app\index\controller;u think\config;u think\request;u think\log;// 日志定位class demo extends ba{    public function logtest() {        $mrs = model("video")->where(["id" => 2])->find();        echo model("video")->getlastsql();        return 1;    }}乘法的初步认识教学设计

变量调试

application/index/controller/demo.php

<?phpnamespace app\index\controller;u think\config;u think\request;u think\log;// 日志定位class demo extends ba{    public function logtest() {        // prinr_r();        // var_dump();        // 类似断点调试        dump(input("param."));        halt(input("param.")); //  dump()  exit;        return 1;    }}

性能调试

application/index/controller/demo.php

<?phpnamespace app\index\controller;u think\config;u think\request;u think\log;// 日志定位u think\debug;//性能调试class demo extends ba{    public function logtest() {                debug("start1");//--开始标记        $mrs = model("video")->where(["id" => 2])->find();        //echo model("video")->getlastsql();                debug("end1");//--结束标记        dump(debug("start1", "end1", 4));//string '0.0241'        dump(debug("start1", "end1", "m"));//string '1.2 mb'        //第三个参数如果是数字代表记录时间;如果是'm'代表记录内存使用        return 1;    }}

sql调试

application/index/controller/demo.php

<?phpnamespace app\index\controller;u think\config;u think\request;u think\log;// 日志定位u think\debug;//性能调试class demo extends ba{    public function logtest() {                $mrs = model("video")->where(["id" => 2])-坪坝>find();        echo model("video")->getlastsql();        return 1;    }}

本文发布于:2023-04-08 03:10:30,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/c3a7ff3b5148f5eabbba8b69caa50260.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:thinkphp5基础.doc

本文 PDF 下载地址:thinkphp5基础.pdf

标签:数据库   路由   控制器   操作
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图