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