ThinkPHP是一個免費開源的,快速、簡單的面向對象的輕量級PHP開發框架。
嚴格來講,ThinkPHP無需安裝過程,這裏所說的安裝其實就是把ThinkPHP框架放入WEB運行環境(前提是你的WEB運行環境已經OK),能夠經過兩種方式獲取和安裝ThinkPHP。php
下載ThinkPHP安裝
獲取ThinkPHP的方式不少,官方網站(http://thinkphp.cn)是最好的下載和文檔獲取來源。
官網提供了穩定版本的下載:http://thinkphp.cn/down/framework.html
因爲ThinkPHP5.0還在測試階段,因此須要經過Git服務器下載,Git服務地址:https://github.com/top-think/think
下載或者使用GIT克隆到本地後,請(解壓縮後)放置於你的WEB根目錄下面的tp5子目錄。
還有一種方式,就是經過Composer進行安裝,這裏再也不介紹了,傳送門:https://www.kancloud.cn/thinkphp/thinkphp5-guide/30549。
不管你採用什麼方式獲取的ThinkPHP框架,如今只須要作最後一步來驗證是否正常運行。
在瀏覽器中輸入地址:
http://localhost/tp5/public/
若是瀏覽器輸出如圖所示:
如今已經完成了ThinkPHP的安裝!html
接下來進一步分析一下ThinkPHP5.0的框架結構:mysql
首先是瀏覽一下ThinkPHP5.0的目錄結構git
tp5 WEB部署目錄(或者子目錄)
├─application 應用目錄
│ ├─common 公共模塊目錄(能夠更改)
│ ├─module_name 模塊目錄
│ │ ├─config.php 模塊配置文件
│ │ ├─common.php 模塊函數文件
│ │ ├─controller 控制器目錄
│ │ ├─model 模型目錄
│ │ ├─view 視圖目錄
│ │ └─ ... 更多類庫目錄
│ │
│ ├─command.php 命令行工具配置文件
│ ├─common.php 公共函數文件
│ ├─config.php 公共配置文件
│ ├─route.php 路由配置文件
│ ├─tags.php 應用行爲擴展定義文件
│ └─database.php 數據庫配置文件
│
├─public WEB目錄(對外訪問目錄)
│ ├─index.php 入口文件
│ ├─router.php 快速測試文件
│ └─.htaccess 用於apache的重寫
│
├─thinkphp 框架系統目錄
│ ├─lang 語言文件目錄
│ ├─library 框架類庫目錄
│ │ ├─think Think類庫包目錄
│ │ └─traits 系統Trait目錄
│ │
│ ├─tpl 系統模板目錄
│ ├─base.php 基礎定義文件
│ ├─console.php 控制檯入口文件
│ ├─convention.php 框架慣例配置文件
│ ├─helper.php 助手函數文件
│ ├─phpunit.xml phpunit配置文件
│ └─start.php 框架入口文件
│
├─extend 擴展類庫目錄
├─runtime 應用的運行時目錄(可寫,可定製)
├─vendor 第三方類庫目錄(Composer依賴庫)
├─build.php 自動生成定義文件(參考)
├─composer.json composer 定義文件
├─LICENSE.txt 受權說明文件
├─README.md README 文件
├─think 命令行入口文件
~~~github
其中入口文件就是 tp5\public\index.php,上面使用的http://localhost/tp5/public/地址,最開始加載的文件就是tp5\public\index.php文件,由於index.php是默認被加載的,因此完整的路徑是http://localhost/tp5/public/index.php。
上面的應用(application)目錄,就是咱們寫邏輯代碼的地方。
ThinkPHP5.0是基於MVC(模型-視圖-控制器)的方式來組織的。
ThinkPHP5.0的URL訪問受路由決定,若是關閉路由或者沒有匹配路由的狀況下,則是基於:sql
http://serverName/index.php(或者其它應用入口文件)/模塊/控制器/操做/參數/值…
而後咱們來看看實際的文件:thinkphp
這是ThinkPHP5.0的默認目錄文件,咱們能夠看到模塊爲index,控制器也是index,操做也是index。數據庫
模塊文件夾下面除了有controller文件夾外,還能夠新增model文件夾(模型),view文件夾(視圖),這些文件夾就是ThinkPHP5中MVC架構的體現。apache
知道這些後,再結合上面URL的完整路徑規則,那麼能夠知道訪問「index操做」的完整路徑是:json
http://localhost/tp5/public/index.php/index/index/index
其中最右邊的index表明操做,右邊第二個index表明控制器,右邊第三個index代碼模塊。
接下來筆者用一個登陸功能來串聯一下這些知識點:
項目結構
數據庫
index.php文件
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
<?php namespace app\index\controller; use app\index\model\User; use think\Db; class Index { public function index() { return 'hello thinkphp5'; } public function loginview(){ return view(); } public function login(){ $name = $_GET["name"]; $pass = $_GET["password"]; $user = new User($name,$pass); //查詢數據庫 $val = Db::table('userinfo') ->where('name',$user->GetName()) ->where('pass',$user->GetPass()) ->find(); $res = ""; if($val){//有該用戶 $_SESSION["user"] = $user->GetName(); $res = "login successed"; }else{//沒有該用戶 $res = "login failed"; } return $res; } }
User.php文件
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
<?php namespace app\index\model; class User{ private $name; private $pass; public function __construct($name,$pass){ $this->name = $name; $this->pass = $pass; } public function GetName(){ return $this->name; } public function GetPass(){ return $this->pass; } }
loginview.html文件
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
<form action = "login"> <div><span>用戶名:</span><input type="text" name="name" /></div> <div><span>密碼:</span><input type="password" name="password"/></div> <div><input type="submit" value="登陸" /></div> </form>
database.php文件
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- return [ // 數據庫類型 'type' => 'mysql', // 服務器地址 'hostname' => '127.0.0.1', // 數據庫名 'database' => 'test', // 用戶名 'username' => 'root', // 密碼 'password' => '', // 端口 'hostport' => '3306', // 鏈接dsn 'dsn' => '', // 數據庫鏈接參數 'params' => [], // 數據庫編碼默認採用utf8 'charset' => 'utf8', // 數據庫表前綴 'prefix' => '', // 數據庫調試模式 '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' => 'Y-m-d H:i:s', // 是否須要進行SQL性能分析 'sql_explain' => false, ];
而後使用 http://localhost/tp5/public/index.php/index/index/loginview 訪問登陸
效果圖: