【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
<?PHP namespace app\index\controller; use think\Config; class Demo extends Base{ // 配置文件的使用 public function config() { 默认一页显示15条 $config = Config::get("paginate"); dump($config);15 $config = config("paginate.type"); dump(config("?paginate"));boolean true ); dump(config("?paginate111"));boolean false 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
application/index/config/PHP
<?PHP [ "cyy" => [ 'cyy' => 1,1)"> ] ];
路由的使用
自定义路由
application/index/controller/Video.PHP
<?PHP namespace app\index\controller; class Video Controller { 自定义路由 getVideo() { $id = input("param.id"); http://PHPtest.com/index/video/getVideo?id=2 // => 域名/video/2 dump($id); } }
application/route.PHP
<? +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2016 http://thinkPHP.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- 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+']],1)"> ]);
控制器的使用
application/index/controller/Demo.PHP
<? think\Config; think\Request; /** * 初始化 * @auth cyy * @return [type] [description] */ _initialize() { dump("这是initialize"); } test() { 数组转json格式返回 return json(["as" => 1,"cyy" => "test"]); } hello() { var_dump(input("param."return "index-index-hello"; } 控制器-跳转 abc() { $id = input("param.id","intval"); if($id == 1) { $this->success("操作成功","admin/index/index"); }elseif($id == 2$this->error("操作失败"); } } 控制器-重定向 ef() { $this->redirect("hello",["id" => 1,"ms" => 123]); redirect("https://baidu.com"); } 控制器-请求 requestData() { $request = Request::instance(); 访问http://PHPtest.com/index/demo/requestData?ids=2 dump(request()->isPost());boolean false dump(input("?get.ids"));boolean true dump(request()->has("ids","get")); } }
application/index/controller/Base.PHP
<? think\Controller; class Base * * 空操作 * @auth singwa * @param [type] $name [description] * @return [type] [description] 控制器-空操作 function _empty($name) { todo return ; } }
数据库配置与model层数据操作
application/database.PHP
<? +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2016 http://thinkPHP.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- [ 数据库类型 'type' => 'MysqL',1)"> 服务器地址 'hostname' => 'localhost',1)"> 数据库名 'database' => 'test',1)"> 用户名 'username' => 'root',1)"> 密码 'password' => '123456',1)"> 端口 'hostport' => '3306',1)"> 连接dsn 'dsn' => '',1)"> 数据库连接参数 'params' => [],1)"> 数据库编码默认采用utf8 'charset' => 'utf8',1)"> 数据库表前缀 'prefix' => 'test_',1)"> 数据库调试模式 'debug' => true,1)"> 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'deploy' => 0,1)"> 数据库读写是否分离 主从式有效 'rw_separate' => false,1)"> 读写分离后 主服务器数量 'master_num' => 1,1)"> 指定从服务器序号 'slave_no' => '',1)"> 是否严格检查字段是否存在 'fields_strict' => 数据集返回类型 'resultset_type' => 'array',1)"> 自动写入时间戳字段 'auto_timestamp' => 时间字段取出后的默认时间格式 'datetime_format' => 是否需要进行sql性能分析 'sql_explain' => ];
模型
application/common/model/Base.PHP
<?PHP namespace app\common\model; think\Model; Model{ protected $autoWriteTimestamp = true; protected $createTime = 'create_a_time'; * * 新增逻辑 * @auth singwa * @param array $data [description] * @return int function add($data = []) { if(empty($data) || !is_array($data)) { return false; } $this->allowField(true)->save($this->id; } }
application/common/model/Video.PHP
<?PHP namespace app\common\model; Base{ * * 定义一个关联 1-1 * @auth singwa * @return [type] [description] videoFile() { $this->hasOne("VideoFile"); video_id } }
application/common/model/VideoFile.PHP
<?class VideoFile Base{ }
application/index/controller/Video.PHP
<? think\Db; use app\common\model\Video as VideoModel; Base { 数据库query查询 function MysqL() { $video = Db::query("select * from test_video where id=2"$video); } 模型的使用 model() { 获取id为2的数据 $video = VideoModel::get(2dump($video); $video = new VideoModel(); $video->title = "cyy-test"; $video->description = "cyy-test-description1"; dump($video->save());//int 1 插入数据保存成功 [ "title" => "cyy-test3",1)"> ]; dump($video->save($data));//dump($video->save());//int 1 插入数据保存成功 增 add() { $video = model("Video"); [ "title" => "cyy-test6","mpt" => 1,1)"> ]; $id = $video->add($id);string '6' (length=1) 查 select() { $conditon = [ "status" => 1,1)"> ]; $videos = model("Video")->where($conditon)->select(); $videos = model("Video") ->where($conditon) ->limit(1) ->order("id","desc") ->select(); dump($videos 改 update() { $updataData = [ "title" => "cyy你好鸭" ]; $whereCondition = [ "id" => 1,1)">$res = model("Video")->allowField(true)->save($updataData,$whereCondition); //echo model("Video")->getLastsql(); //作用非常重要 //dump($res); model("Video")->where($whereCondition) ->update($updataDataecho model("Video")->getLastsql();UPDATE `test_video` SET `title`='cyy你好鸭' WHERE `id` = 1 删 delete() { 这种场景是 真正的删除 $res = model("Video")->where("id",">",18)->delete(); echo model("Video")->getLastsql(); dump($res);*/ 在实际工作当中 我们的删除一般不直接删除, 所以一般假删除 // 修改status => -1 model("Video")->where(["id" => 6]) ->update(["status" => -1UPDATE `test_video` SET `status`=-1 WHERE `id` = 6 } 一对一关联 correlation() { $video = model("Video")->find(1halt($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->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)->select()); //dump(VideoModel::hasWhere('videoFile',["video_id" => 1])->select()); } }
视图层
application/index/controller/Video.PHP
<? demo() { $video = model("Video")->where(["id" => 1])->find(); $videos = model("Video")->where("id",1)->select(); halt($video->update_time);//halt=dump+exit // halt($video->update_time); // halt($video->toArray()); $this->fetch("",1)"> [ "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']} {$names.name} {$video->title} {$video.title} {$video:title} {eq name="name",value="cyy1"} cyy您好1111 {else /} 不是cyy {/eq} {$Request.get.id} {$video->create_time|date="Y-m-d H",###}br {$video->status|status} {volist name="videos" id="vo"} {$vo.id} -------- {$vo.title} br / {/volist} </>
日志定位
application/index/controller/Demo.PHP
<? think\Request; use think\Log; 日志定位 logtest() { Log::write("testlog".json_encode(input("param."))); return 1; } }
[ 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=45log ] 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' => ( 0 => 'index',1 => 'demo',2 => 'logtest',1)"> ),1)"> ) [ info ] [ HEADER ] ( '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','user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/77.0.3865.75 Safari/537.36','upgrade-insecure-requests' => '1','cache-control' => 'no-cache','pragma' => 'no-cache','connection' => 'close','host' => 'PHPtest.com',1)"> ) [ info ] [ PARAM ] ( 'mst' => '1','mp' => '45',1)"> ) [ 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' => 应用Trace 'app_trace' => PHP
<? logtest() { $mrs = model("Video")->where(["id" => 2])->find(); echo model("Video")->getLastsql(); ; } }
变量调试
application/index/controller/Demo.PHP
<? prinr_r(); // var_dump(); // 类似断点调试 dump(input("param.")); halt(input("param.")); dump() exit; ; } }性能调试
application/index/controller/Demo.PHP
<? 日志定位 use think\Debug;性能调试 logtest() { debug("start1");--开始标记 echo model("Video")->getLastsql(); debug("end1");--结束标记 dump(debug("start1","end1",4));string '0.0241' dump(debug("start1","m"));string '1.2 MB' //第三个参数如果是数字代表记录时间;如果是'm'代表记录内存使用 ; } }sql调试
application/index/controller/Demo.PHP
原文链接:https://www.f2er.com/thinkphp/881440.html<? logtest() { ; } }