標籤:pac tab sql index [] 解析 nbsp comm namespace
1.目錄結構:
其中thinkphp子目錄是架構核心目錄
thinkphp結構:
2.入口檔案
預設內建的入口檔案位於public/index.php
應用目錄為application,其結構:
index模組目錄結構:
Index為控制器檔案;
3.控制器:
找到index模組的Index控制器;
找到index模組的Index控制器
去把傳回值變為helloworld
訪問:
看到輸出結果!
4.資料的讀取:
資料庫:
需要在應用的資料庫設定檔application/database.php中添加資料庫的串連資訊如下:
<?phpreturn [ ‘type‘ => ‘mysql‘, // 資料庫類型 ‘hostname‘ => ‘127.0.0.1‘, // 伺服器位址 ‘database‘ => ‘outengcms‘, // 資料庫名 ‘username‘ => ‘root‘, // 使用者名稱 ‘password‘ => ‘root‘, // 密碼 ‘hostport‘ => ‘3306‘, // 連接埠 ‘dsn‘ => ‘‘, // 串連dsn ‘params‘ => [], // 資料庫連接參數 ‘charset‘ => ‘utf8‘, // 資料庫編碼預設採用utf8 ‘prefix‘ => ‘think_‘, // 資料庫表首碼 ‘debug‘ => true, // 資料庫偵錯模式 ‘deploy‘ => 0, // 資料庫部署方式:0 集中式(單一伺服器),1 分布式(主從伺服器) ‘rw_separate‘ => false, // 資料庫讀寫是否分離 主從式有效 ‘master_num‘ => 1, // 讀寫分離後 主伺服器數量 ‘slave_no‘ => ‘‘, // 指定從伺服器序號 ‘fields_strict‘ => true, // 是否嚴格檢查欄位是否存在 ‘resultset_type‘ => ‘array‘, // 資料集傳回型別 array 數組 collection Collection對象 ‘auto_timestamp‘ => false, // 是否自動寫入時間戳記欄位 ‘sql_explain‘ => false, // 是否需要進行SQL效能分析];
接下來,我們修改下控制器方法,添加讀取資料的代碼:
<?phpnamespace app\silingling\controller;use think\Controller;//use think\Db;class Index extends Controller{ public function _empty($name) { return $this->fetch(‘/Public/404‘); } public function tianjia($code=‘‘){ if(!captcha_check($code)) { $this->error(‘驗證碼錯誤‘); }//// echo 111111111;////添加資料庫1 else { $naa = $_POST["naa"]; $tel = $_POST["tel"]; //留言聯絡我們 Db::table(‘think_shenqing‘) ->data([‘naa‘=>$naa,‘tel‘=>$tel]) ->insert(); $this->success(‘添加成功‘,‘index‘); } // // }}
控制器寫好後,直接修改模版檔案,用標籤顯示就可以了;
5.URL訪問
ThinkPHP採用單一入口模式訪問應用,對應用的所有請求都定向到應用的入口檔案,系統會從URL參數中解析當前請求的模組、控制器和操作,下面是一個標準的URL訪問格式:
http://serverName/index.php/模組/控制器/操作
應用下面的子目錄稱之為模組,模組全部採用小寫命名
應用的index模組的Index控制器定義如下:
<?phpnamespace app\silingling\controller;use think\Controller;//use think\Db;class Index extends Controller{ public function _empty($name) { return $this->fetch(‘/Public/404‘); } public function index() { return $this->fetch(‘/Public/index‘); } public function index1() { $list=Db::name(‘auth_rule‘)->where(‘sort‘, 55)->select(); $this->assign(‘list‘,$list);// liucheng$list3 = Db::name(‘article‘)->where(‘writer‘,22)->select();$this->assign(‘list3‘,$list3);//chaxun$list211 = Db::name(‘haoma‘)->where(‘code‘>0)->select();$this->assign(‘list211‘,$list211); return $this->fetch(‘/Public/index1‘); }}
如果我們直接存取入口檔案index,因為我們沒有指定url,所以系統會訪問預設模組(index)下面的預設控制器(Index)的預設操作方法(index),
http://localhost/index.php
http://localhost/index.php/index/index/index
這兩個串連等效!
應用的index模組的Index控制器定義如下:
<?phpnamespace app\lianxi\controller;use think\Controller;use think\Db;class Index extends Controller{ public function _empty($name) { return $this->fetch(‘/Public/404‘); } public function index(){ return ‘index‘; } public function hello($name = ‘World‘){ return ‘Hello,‘ . $name . ‘!‘; } }
如果我們直接存取入口檔案的話,預設走的是index方法,
如果要存取控制器的hello方法,則需要使用完整的URL地址
輸出的是:
由於name參數為選擇性參數,串連這樣輸:
http://localhost/index.php/lianxi/Index/hello/name/xuanxuan
輸出:
6.模板渲染輸出:
輸出當前模組下的index模板:
-
// 指定模板輸出
-
$this->display(‘index‘);
輸出User模組下面的read模板:
-
$this->display(‘User:read‘);
輸出模板時指定編碼和類型:
-
// 表示輸出XML頁面類型(注意:這裡可以輸出網站地圖sitemap.xml哦~~)
-
$this->display(‘read‘, ‘utf-8‘, ‘text/xml‘);
總結一下,ThinkPHP的模板渲染可以設定編碼類別型及輸出檔案的類型
thphp5.0學習筆記(一)