【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 $config = config("paginate.type"); 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
<?php // 組態檔的使用 return [ "host" => "127.0.0.1", ];
application/index/config/php
<?php // 組態檔的使用 return [ "cyy" => [ 'cyy' => 1, ] ];
路由的使用
自定義路由
application/index/controller/Video.php
<?php namespace app\index\controller; class Video extends Controller { // 自定義路由 public function getVideo() { $id = input("param.id"); // http://phptest.com/index/video/getVideo?id=2 // => 域名/video/2 dump($id); } }
application/route.php
<?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 <[email protected]> // +---------------------------------------------------------------------- use 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
<?php namespace app\index\controller; use think\Config; use think\Request; class Demo extends Base{ /** * 初始化 * @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"); }elseif($id == 2) { $this->error("操作失敗"); } } // 控制器-重定向 public function ef() { $this->redirect("hello", ["id" => 1, "ms" => 123]); //redirect("https://baidu.com"); } // 控制器-請求 public function 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"));//boolean true } }
application/index/controller/Base.php
<?php namespace app\index\controller; use think\Controller; class Base extends Controller { /** * 空操作 * @auth singwa * @param [type] $name [description] * @return [type] [description] */ // 控制器-空操作 public function _empty($name) { // todo return $name; } }
資料庫配置與model層資料操作
application/database.php
<?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 <[email protected]> // +---------------------------------------------------------------------- return [ // 資料庫型別 'type' => 'mysql', // 服務器地址 'hostname' => 'localhost', // 資料庫名 'database' => 'test', // 用戶名 'username' => 'root', // 密碼 'password' => '123456', // 埠 'hostport' => '3306', // 連接dsn 'dsn' => '', // 資料庫連接引數 'params' => [], // 資料庫編碼默認采用utf8 'charset' => 'utf8', // 資料庫表前綴 'prefix' => 'test_', // 資料庫除錯模式 'debug' => true, // 資料庫部署方式:0 集中式(單一服務器),1 分布式(主從服務器) 'deploy' => 0, // 資料庫讀寫是否分離 主從式有效 'rw_separate' => false, // 讀寫分離后 主服務器數量 'master_num' => 1, // 指定從服務器序號 'slave_no' => '', // 是否嚴格檢查欄位是否存在 'fields_strict' => true, // 資料集回傳型別 'resultset_type' => 'array', // 自動寫入時間戳欄位 'auto_timestamp' => false, // 時間欄位取出后的默認時間格式 'datetime_format' => false, // 是否需要進行SQL性能分析 'sql_explain' => false, ];
模型
application/common/model/Base.php
<?php namespace app\common\model; use think\Model; class Base 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 false; } $this->allowField(true)->save($data); return $this->id; } }
application/common/model/Video.php
<?php namespace app\common\model; class Video extends Base{ /** * 定義一個關聯 1-1 * @auth singwa * @return [type] [description] */ public function videoFile() { return $this->hasOne("VideoFile"); // video_id } }
application/common/model/VideoFile.php
<?php namespace app\common\model; class VideoFile extends Base{ }
application/index/controller/Video.php
<?php namespace app\index\controller; use think\Db; use app\common\model\Video as VideoModel; class Video extends Base { // 資料庫query查詢 public function mysql() { $video = Db::query("select * 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 select() { $conditon = [ "status" => 1, ]; //$videos = model("Video")->where($conditon)->select(); $videos = model("Video") ->where($conditon) ->limit(1) ->order("id", "desc") ->select(); 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` SET `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` SET `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)->select()); //dump(VideoModel::hasWhere('videoFile', ["video_id" => 1])->select()); } }
視圖層
application/index/controller/Video.php
<?php namespace app\index\controller; use think\Db; use app\common\model\Video as VideoModel; class Video extends Base { public function 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()); 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 /> {eq name="name", value="https://www.cnblogs.com/chenyingying0/p/cyy1"} cyy您好1111<br /> {else /} 不是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
<?php namespace app\index\controller; use think\Config; use think\Request; use think\Log;// 日志定位 class Demo extends Base{ 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', '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', ) [ 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
<?php namespace app\index\controller; use think\Config; use think\Request; use think\Log;// 日志定位 class Demo extends Base{ public function logtest() { $mrs = model("Video")->where(["id" => 2])->find(); echo model("Video")->getLastSql(); return 1; } }

變數除錯
application/index/controller/Demo.php
<?php namespace app\index\controller; use think\Config; use think\Request; use think\Log;// 日志定位 class Demo extends Base{ public function logtest() { // prinr_r(); // var_dump(); // 類似斷點除錯 dump(input("param.")); halt(input("param.")); // dump() exit; return 1; } }
性能除錯
application/index/controller/Demo.php
<?php namespace app\index\controller; use think\Config; use think\Request; use think\Log;// 日志定位 use think\Debug;//性能除錯 class Demo extends Base{ 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
<?php namespace app\index\controller; use think\Config; use think\Request; use think\Log;// 日志定位 use think\Debug;//性能除錯 class Demo extends Base{ public function logtest() { $mrs = model("Video")->where(["id" => 2])->find(); echo model("Video")->getLastSql(); return 1; } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/84996.html
標籤:PHP
上一篇:Go 與 PHP 的語法對比
下一篇:thinkphp論壇專案開發
