在前面,我們安裝了thinkphp之后,那么如何用thinkphp開發項目呢?
1、 打開application/index/controller/Index.php,我們可以看到有如下代碼。
<?php Namespace appindexcontroller; class Index { public function index() { return '<style type="text/css">*{?padding:?0;?margin:?0;?}?div{?padding:?4px?48px;}?a{color:#2E5CD5;cursor:?pointer;text-decoration:?none}?a:hover{text-decoration:underline;?}?body{?background:?#fff;?font-family:?"Century?Gothic","Microsoft?yahei";?color:?#333;font-size:18px;}?h1{?font-size:?100px;?font-weight:?normal;?margin-bottom:?12px;?}?p{?line-height:?1.6em;?font-size:?42px?}<div>?<h1>:)?</h1> <p>?thinkphp?V5.1<br><span>12載初心不改(2006-2018)?-?你值得信賴的PHP框架</span></p> </div><script></script><script></script><think></think>'; ????} ????public?function?hello($name?=?'ThinkPHP5') ????{ ????????return?'hello,'?.?$name; ????} }
在上述代碼中,
????(1)、namespace appindexcontroller 是命名空間。PHP中命名空間使用關鍵字namespace定義,其基本語法格式是:
立即學習“PHP免費學習筆記(深入)”;
????????namespace?空間名稱;
????????其中空間名稱遵循基本標識符命名規則,以數字、字母和下劃線構成,且不能以數字開頭)。關于更多的命名空間,大家可以自行上網搜索。
????(2)、class Index 是一個類,類中有index()和hello()方法,比如index()方法中的return返回的就是我們項目首頁的html內容。
????(3)、訪問index()方法,直接通過http://localhost:8010/tp5.1.36/public這個URL進行訪問(localhost表示的是本地主機,8010是apache服務器的端口號,tp5.1.36是項目名)。
????(4)、如果要訪問hello()方法,那么就需要通過http://localhost:8010/tp5.1.36/public/index.php/index/index/hello這個URL來訪問,打開后,網頁中顯示“hello,ThinkPHP5”.
????????ThinkPHP5.1完全開放手冊是這樣描述的:http://serverName/index.php(或者其它應用入口文件)/模塊/控制器/操作/[參數名/參數值…]
????那么我們來看這個地址
????http://localhost:8010/tp5.1.36/public/index.php/index/index/hello
????其中:
????????index.php后面的第一個index表示的是模塊;
????????index.php后面的第二個index表示的是控制器;
????????hello表示的是index模塊下的index控制器下的hello()方法。
????(5)、可以通過URL重寫隱藏應用的入口文件index.php(也可以是其它的入口文件,但URL重寫通常只能設置一個入口文件),下面是相關服務器的配置參考(以apache為例):
????????1)??? httpd.conf配置文件中加載了mod_rewrite.so模塊
????????2)??? AllowOverride None 將None改為 All
????????3)??? 把下面的內容保存為.htaccess文件放到應用入口文件的同級目錄下
????<ifmodule> ??????Options?+FollowSymlinks?-Multiviews ??????RewriteEngine?On ? ??????RewriteCond?%{REQUEST_FILENAME}?!-d ??????RewriteCond?%{REQUEST_FILENAME}?!-f ??????RewriteRule?^(.*)$?index.php/$1?[QSA,PT,L] ????</ifmodule>
2、 打開mysql服務器,創建數據庫,將數據庫名稱命名為student。
3、 在該數據庫下創建一張表,表名為:student。
?
4、 向student表中插入如下數據
上面性別的取值中,1表示的是男,2表示的是女。
5、 編輯config/database.php文件,參考如下代碼修改數據庫的配置。
????//?數據庫類型 ????'type'????????????=>?'mysql', ????//?服務器地址 ????'hostname'????????=>?'127.0.0.1', ????//?數據庫名 ????'database'????????=>?'student', ????//?用戶名 ????'username'????????=>?'root', ????//?密碼 ?????'password'????????=>?'root',
????請根據自己的實際情況進行修改,我這里的用戶名和密碼都是root。
6、 在Index控制器下添加student()方法,將student表查詢出來,具體代碼如下。
???public?function?index() ??{ ?????$data=thinkDb::name(‘select?*?from?`student`’); ?????$arr=[]; ????????foreach($data?as?$v){ ?????????????$arr[]=$v[‘name’]; ????????} ?????return?implode(‘,’,$arr); ????}
????????通過訪問localhost:8010/tp5.1.36/public/index.php/index/index/student進行測試,可以看到瀏覽器中顯示的數據為:李四,張三,王五。
7、 在實際開發中,我們會遇到各種錯誤,為了更好的調試錯誤,ThinkPHP提供了非常強大的錯誤報告和跟蹤調試功能。打開config/app.php文件,找到如下兩行代碼,將值改為true。
????‘app_debug’?=>’true’,//應用調試模式 ????‘app_trace’?=>’true’,//應用trace
8、 在實際開發中,需要編寫大量的HTML網頁,為了方便編寫HTML網頁,我們可以單獨將HTML放置在一個模板文件中。為了實現這個效果,需要讓控制器中的Index類繼承thinkController類,代碼如下所示。
????class?Index?extends?thinkController
9、 繼承thinkController類后,就可以使用這個類提供的assgin()和fetch()方法。
10、 接下來修改student()方法中的代碼內容,調用assgin()方法為模板賦值,再調用fetch()方法喧嚷模板,具體代碼如下。
????public?function?index() ????{ ??????$data=thinkDb:?name(‘student`’)->filed(‘name’)->select(); ????????????$this->assgin(‘data’,$data); ????????return?$this->fetch(); ????}
????????通過訪問localhost:8010/tp5.1.36/public/index.php/index/index/student進行測試,會出現報錯,是因為我們還沒有創建該模板,根據提示可以找到該路徑位于application/index/view/Index/student.html。手動創建模板文件和其所在的目錄,編寫代碼如下:
????nbsp;html> ???? ???????? ????????<meta> ????????<meta> ????????<meta> ????????<title>學生信息列表</title> ???????? ???????? ???????{?foreach($data?as?$v)} ??????????<div>{$v.name]}></div> ???????{/foreach;} ????????
11、這樣就可以在模板文件中輸出所有學生的姓名。
TP5.1的第一個項目就這樣完成了,在后續的文章中,我們再進行細講涉及到的知識點。