下面thinkphp框架教程欄目將給大家講解ThinkPHP5 的簡(jiǎn)單搭建和使用,希望對(duì)需要的朋友有所幫助!
0X01 Thinkphp 的安裝
我這里選擇的是使用 windows 下的 composer 進(jìn)行安裝,收下首先下載 composer 這個(gè)工具,安裝完成以后進(jìn)入我們想要?jiǎng)?chuàng)建項(xiàng)目的文件夾輸入下面的命令
composer create-project topthink/think tp5 dev-master --prefer-dist
這樣就會(huì)在當(dāng)前目錄下形成一個(gè) 名為 tp5 的文件夾,這個(gè)文件夾中存放的就是 thinkphp5 的基本的框架
0X02 重點(diǎn)目錄結(jié)構(gòu)及文件介紹
1.目錄結(jié)構(gòu)
application : 應(yīng)用目錄,我們的模型視圖控制器都會(huì)放在這個(gè)文件夾下,這是我們開(kāi)發(fā)的主陣地
public : 這個(gè)是我們項(xiàng)目的入口文件,thinkphp 是一個(gè)單一入口的框架
thinkphp : 框架的核心目錄
2.關(guān)鍵文件
application/config.php 項(xiàng)目配置文件,開(kāi)啟 debug 調(diào)試模式(在開(kāi)發(fā)中)
application/database.php 數(shù)據(jù)庫(kù)配置文件
public/index.php 項(xiàng)目入口文件,定義了應(yīng)用目錄的位置以及包含框架啟動(dòng)文件來(lái)啟動(dòng)框架
0X03 配置虛擬主機(jī)
1.httpd.conf 中判斷下面是否被注釋?zhuān)绻蛔⑨屨?qǐng)取消注釋
(1)Include conf/vhosts.conf (2)LoadModule vhost_alias_module modules/mod_vhost_alias.so
2.刪除 vhost.conf 中原有的默認(rèn)內(nèi)容,添加如下內(nèi)容
<VirtualHost *:80> DocumentRoot "E:phpstudyPHPTutorialWWWtp5public" ServerName localhost <Directory "E:phpstudyPHPTutorialWWWtp5public"> Options FollowSymLinks ExecCGI AllowOverride All Order allow,deny Allow from all Require all granted </Directory> </VirtualHost>
3.配置 URL 重寫(xiě)
http.conf 中解開(kāi)下面的注釋
LoadModule rewrite_module modules/mod_rewrite.so
并在虛擬主機(jī)配置中寫(xiě)上
AllowOverride All
注意:如果使用 phpstudy 的話(huà),官方默認(rèn)的 .htaccess 是不可以的,需要修改成下面這個(gè)樣子
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1] </IfModule>
0X04 基本的寫(xiě)法
1.控制器的基本寫(xiě)法
(1)模塊中的控制器實(shí)際上就是一個(gè)一個(gè)的類(lèi),這個(gè)類(lèi)寫(xiě)的時(shí)候要繼承 Controller 并且要在前面寫(xiě)上命名空間
(2) thinkPHP5 使用 return 來(lái)返回一個(gè)html ,自動(dòng)渲染到頁(yè)面上
(3)tp5 使用的是 $this->requrst->param() 接受參數(shù),當(dāng)然也要在開(kāi)始寫(xiě)上命名空間
示例代碼:
<?php namespace appindexcontroller; use thinkController; use thinkRequest; class Index extends Controller { public function index() { print_r($this->request->param()); return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ 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 }</style><p style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一劍 - 為API開(kāi)發(fā)設(shè)計(jì)的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" rel="external nofollow" target="qiniu">七牛云</a> 獨(dú)家贊助發(fā)布 ]</span></p><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>'; } }
我們這樣訪(fǎng)問(wèn)
http://localhost/index.php/index/index/index/a/3/b/4/c/5
結(jié)果:
2.模板和控制器的關(guān)系
每一個(gè)模塊都有自己的控制器、視圖、和模型,訪(fǎng)問(wèn)的時(shí)候是按照 index.php/模塊/控制器/方法,訪(fǎng)問(wèn)的,然后每一個(gè)控制器在 view 中對(duì)應(yīng)著一個(gè)同名的文件夾,比如說(shuō) controller/Index 控制器, view/Index 就是這個(gè)控制器對(duì)應(yīng)的模板文件夾,那么每一個(gè)方法都會(huì)在模板文件夾下對(duì)應(yīng)一個(gè)同名的 html 文件作為這個(gè)方法的模板
tp5 是通過(guò)
$this->assign('data',$data);
進(jìn)行賦值并通過(guò)
return $this->fetch('模板名');
進(jìn)行渲染的
示例代碼:
index/controller/Index.php
<?php namespace appindexcontroller; use thinkController; class Index extends Controller { public function index() { $data = "K0rz3n"; $this->assign('data',$data); return $this->fetch(); } }
Index/view/Index/index.html
<html> <head> </head> <body> hello {$data}! </body> </html>
3.對(duì) SEO 友好的路由
我們知道,我們的搜索引擎抓取頁(yè)面最多抓三層,但是我們剛剛寫(xiě)的那種 URL 已經(jīng)太多層了,這非常不利于搜索引擎的收錄,于是 tp5 給我們提供了一種簡(jiǎn)化的方法,就是 route.php
示例代碼:
return [ '__pattern__' => [ 'name' => 'w+', ], '[hello]' => [ // ':id' => ['index/hello', ['method' => 'get'], ['id' => 'd+']], // ':name' => ['index/hello', ['method' => 'post']], ], 'hello/[:name]' => ['index/Index/hello',['method' => 'get','ext' => 'html']], ];
這個(gè)意思就是我們?cè)L問(wèn) hello/name 就會(huì)轉(zhuǎn)給 index/Index/hello ,并且要求是 Get 方法,后綴名是 HTML
配置好后我們只要添加這樣幾個(gè)東西就 OK 了
public function hello($name = 'zhangsan') { $this->assign('name',$name); return $this->fetch(); }
hello.html
<html> <head> </head> <body> hello {$name}! </body> </html>
如圖所示:
當(dāng)然在這種情況下參數(shù)名還是會(huì)很多斜杠,還是不是很友好,于是我們可以在 config.php 中將默認(rèn)的斜杠分隔符進(jìn)行修改,改成其他的這樣就避免了這個(gè)問(wèn)題
4.URL 自動(dòng)生成
tp5 給我們提供了 url() 這個(gè)函數(shù)幫我們自動(dòng)生成 Url
public function url() { echo url('url2','a=1&b=2'); }
這個(gè)方法運(yùn)行的結(jié)果就是
/index/index/url2/a/1/b/2.html
5.請(qǐng)求和響應(yīng)
1.接收請(qǐng)求的參數(shù)
訪(fǎng)問(wèn): http://localhost/index/index/req/username/test
通過(guò)以下代碼可以得到 username
echo $this->request->param('username');
或者我們可以使用函數(shù)助手 input(),下面這段代碼能達(dá)到和上面一樣的效果
echo input('username');
包括我們通過(guò)下面的代碼獲取 url
echo $this->request->url();
這個(gè)也有自己的函數(shù)助手
echo request()->url();
我們可以獲分別獲取 get post cookie file 等方式的參數(shù)
$this->request->get() $this->request->post() $this->request->cookie() $this->request->file()
或者實(shí)例化一個(gè) Request 對(duì)象,但是這種方法只能接受 url 后面是 & 連接的參數(shù),重寫(xiě)的好像不行
$Request = Request::instance() $request->get() $Rquest->post() $Request->cookie() $Request->file()
2.綁定參數(shù)
$this->request->bind('user',"hh"); echo $this->request->user;
那么為什么請(qǐng)求還要?jiǎng)討B(tài)地綁定參數(shù)呢?因?yàn)楹芏鄷r(shí)候需要傳遞 session 的值,來(lái)維持會(huì)話(huà)
3.返回值
可以返回多種格式的值 比如 json xml 或者通過(guò) $this->fetch() 來(lái)進(jìn)行模板渲染
return json($data); return xml($data);
當(dāng)然我們的 tp 也有對(duì)一些東西的封裝,比如實(shí)現(xiàn)輸出一段話(huà)然后進(jìn)行跳轉(zhuǎn)到某個(gè)方法,或者是直接進(jìn)行重定向
return json($data); return xml($data);
6.模板與輸出
一般的模板渲染就不想介紹了,這里說(shuō)下模板布局,其實(shí)就是在 view 文件夾下有一個(gè) layout.html 文件,這個(gè)文件的內(nèi)容是這樣的
layout.html
{include file="/index/header"/} {__CONTENT__} {include file="/index/footer"/}
然后我們寫(xiě)模板的時(shí)候就在最上面加上對(duì)這個(gè)文件的引用
{layout name="layout"/}
如果我們想全局引入頁(yè)眉頁(yè)腳,這個(gè)配置需要在 config.php 中進(jìn)行設(shè)置,在模板配置中添加下面的代碼
'layout_on' => 'true', 'layout_name' => 'layout', 'layout_item' => '{__CONTENT__}',
這樣的話(huà)就是進(jìn)行了全配置但是如果我們有些頁(yè)面不想這樣配置的話(huà)我們需要在這樣的頁(yè)面上寫(xiě)上
{__NOLAYOUT__}
如果我們模板文件中的靜態(tài)文件路徑想要不寫(xiě)死的話(huà),我們可以在 php 文件中的 fecth 前設(shè)置字符替換
$this->view->replace(['__PUBLIC__' => '/static',]);
如果我們想每個(gè)方法都使用這個(gè)操作,我們就把上面這段代碼放到 控制器的構(gòu)造函數(shù)里面
function __construct(){ parent::__construct(); $this->view->replace(['__PUBLIC__' => '/static',]); }
0X05 參考
https://www.kancloud.cn/thinkphp/thinkphp5-guide/30551