下載地址:http://www.thinkVeVb.com/
本次使用thinkphp5,我采用github進行安裝。
Github
應(yīng)用項目: https://github.com/top-think/think
核心框架: https://github.com/top-think/framework
另外還有:
碼云 :
應(yīng)用項目: https://git.oschina.net/liu21st/thinkphp5.git
核心框架: https://git.oschina.net/liu21st/framework.git
Coding:
應(yīng)用項目: https://git.coding.net/liu21st/thinkphp5.git
核心框架: https://git.coding.net/liu21st/framework.git
下載完成的目錄:
tp5 ├─application 應(yīng)用目錄 ├─extend 擴展類庫目錄(可定義) ├─html' target='_blank'>public 網(wǎng)站對外訪問目錄 ├─runtime 運行時目錄(可定義) ├─vendor 第三方類庫目錄(Composer) ├─thinkphp 框架核心目錄 ├─build.php 自動生成定義文件(參考) ├─composer.json Composer定義文件 ├─LICENSE.txt 授權(quán)說明文件 ├─README.md README 文件 ├─think 命令行工具入口
核心框架目錄的結(jié)構(gòu)如下:
├─thinkphp 框架系統(tǒng)目錄 │ ├─lang 語言包目錄 │ ├─library 框架核心類庫目錄 │ │ ├─think think 類庫包目錄 │ │ └─traits 系統(tǒng) traits 目錄 │ ├─tpl 系統(tǒng)模板目錄 │ ├─.htaccess 用于 apache 的重寫 │ ├─.travis.yml CI 定義文件 │ ├─base.php 框架基礎(chǔ)文件 │ ├─composer.json composer 定義文件 │ ├─console.php 控制臺入口文件 │ ├─convention.php 慣例配置文件 │ ├─helper.php 助手函數(shù)文件(可選) │ ├─LICENSE.txt 授權(quán)說明文件 │ ├─phpunit.xml 單元測試配置文件 │ ├─README.md README 文件 │ └─start.php 框架引導(dǎo)文件2.運行
我使用的是kali自帶的apache2服務(wù)器,使用 service apache2 start 啟動,需要把git下來的整個項目放到服務(wù)器運行目錄下,linux默認是:
/var/www/html
然后在瀏覽器端輸入:http://localhost/tp5/public/
即可看到歡迎頁面:
如果你不想安裝任何 WEB 服務(wù)器,也可以直接使用PHP自帶的 WebServer ,并且運行 router.php 來運行測試。
進入命令行,進入 tp5/public 目錄后,輸入如下命令:
php -S localhost:8888 router.php
接下來可以直接訪問
http://localhost:8888
我們關(guān)注最多的就是應(yīng)用目錄:
├─application 應(yīng)用目錄(可設(shè)置) │ ├─index 模塊目錄(可更改) │ │ ├─config.php 模塊配置文件 │ │ ├─common.php 模塊公共文件 │ │ ├─controller 控制器目錄 │ │ ├─model 模型目錄 │ │ └─view 視圖目錄 │ ├─command.php 命令行工具配置文件 │ ├─common.php 應(yīng)用公共文件 │ ├─config.php 應(yīng)用配置文件 │ ├─tags.php 應(yīng)用行為擴展定義文件 │ ├─database.php 數(shù)據(jù)庫配置文件 │ └─route.php 路由配置文件
5.0版本采用模塊化的設(shè)計架構(gòu),默認的應(yīng)用目錄下面只有一個 index 模塊目錄,如果要添加新的模塊可以使用控制臺命令來生成。切換到命令行模式下,進入到應(yīng)用根目錄(tp5下面)并執(zhí)行如下指令:
php think build --module demo
就會生成一個默認的demo模塊,包括如下目錄結(jié)構(gòu):
├─demo │ ├─controller 控制器目錄 │ ├─model 模型目錄 │ ├─view 視圖目錄 │ ├─config.php 模塊配置文件 │ └─common.php 模塊公共文件 同時也會生成一個默認的 Index 控制器文件。4.模板渲染
首先是Controller:
位于application/index/controller/Index.php有一個默認的Index類:
本來它return的是開始頁面,現(xiàn)在改為hello world。
?phpnamespace app/index/controller;class Index{ public function index() return Hello,World! }
然后我們再繼承Controller類:
?phpnamespace app/index/controller;use think/Controller;//引入Controller類class Index extends Controller{ public function index($name= world ) $this- assign( name ,$name); return $this- fetch();}
我們向頁面?zhèn)鬟f一個帶有默認值的參數(shù)name。
然后是View:
thinkphph采用模板渲染,模板存在View文件夾下,默認是沒有View文件夾的,我們自己創(chuàng)建:
在application/index 目錄下面創(chuàng)建一個 view 目錄,在view目錄下再建一個index目錄,然后添加模板文件hello.html,整個路徑: view/index/hello.html
html head title hello {$name} /title /head body hello {$name}! /body /html
然后我們可以訪問:
或者采用省略路徑:http://localhost/tp5/public/
更高級的可以配置url的路由。
這里采用Mysql數(shù)據(jù)庫,在test表下建一個數(shù)據(jù)庫:
create table if not exists think_data( id int(8) not null auto_increment primary key, data varchar(255) not null )engine=MyISAM default charset=utf8;
再插入幾條數(shù)據(jù)就行;
然后在 application/database.php下進行配置:
return [ // 數(shù)據(jù)庫類型 type = mysql , // 服務(wù)器地址 hostname = 127.0.0.1 , // 數(shù)據(jù)庫名 database = test , // 用戶名 username = root , // 密碼 password = , // 端口 hostport = , // 連接dsn dsn = , // 數(shù)據(jù)庫連接參數(shù) params = [], // 數(shù)據(jù)庫編碼默認采用utf8 charset = utf8 , // 數(shù)據(jù)庫表前綴 prefix = think_ , // 數(shù)據(jù)庫調(diào)試模式 debug = true,
修改controller下的Index類:
?phpnamespace app/index/controller;use think/Controller;use think/Db;//引入數(shù)據(jù)庫class Index extends Controller{ public function index($name= world ) $this- assign( name ,$name); return $this- fetch(); } public function dbtest() $data = Db::name( data )- find(); $this- assign( result ,$data); return $this- fetch();}
再在view下的index目錄下建一個dbtest.html渲染:
html head title /title /head body {$result.id---$result.data} /body /html
再訪問http://localhost/tp5/public/index.php/index/index/dbtest即可。
本文講解了ThinkPHP5快速入門 方法,更多相關(guān)內(nèi)容請關(guān)注php 。
相關(guān)推薦:
介紹ThinkPHP使用步驟
鎖不住的查詢
講解更新鎖(U)與排它鎖(X)的相關(guān)知識
以上就是ThinkPHP5快速入門 方法的介紹的詳細內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。
新聞熱點
疑難解答
圖片精選