0X01 Thinkphp 的安裝
我這里選擇的是使用 windows 下的 composer 進(jìn)行安裝,收下首先下載 composer 這個工具,安裝完成以后進(jìn)入我們想要創(chuàng)建項目的文件夾輸入下面的命令
composer create-project topthink/think tp5 dev-master --prefer-dist
這樣就會在當(dāng)前目錄下形成一個 名為 tp5 的文件夾,這個文件夾中存放的就是 thinkphp5 的基本的框架
0X02 重點目錄結(jié)構(gòu)及文件介紹
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 配置虛擬主機(jī)
1.httpd.conf 中判斷下面是否被注釋,如果被注釋請取消注釋
(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:\phpstudy\PHPTutorial\WWW\tp5\public"
ServerName localhost
Directory "E:\phpstudy\PHPTutorial\WWW\tp5\public">
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
并在虛擬主機(jī)配置中寫上
注意:如果使用 phpstudy 的話,官方默認(rèn)的 .htaccess 是不可以的,需要修改成下面這個樣子
IfModule mod_rewrite.c>
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ù),當(dāng)然也要在開始寫上命名空間
示例代碼:
?php
namespace app\index\controller;
use think\Controller;
use think\Request;
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>div style="padding: 24px 48px;"> h1>:)/h1>p> ThinkPHP V5br/>span style="font-size:30px">十年磨一劍 - 為API開發(fā)設(shè)計的高性能框架/span>/p>span style="font-size:22px;">[ V5.0 版本由 a rel="external nofollow" target="qiniu">七牛云/a> 獨家贊助發(fā)布 ]/span>/div>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>';
}
}
我們這樣訪問
http://localhost/index.php/index/index/index/a/3/b/4/c/5
結(jié)果:
![](/d/20211017/413c16d31e7e70cb42b5c242d297afc9.gif)
2.模板和控制器的關(guān)系
每一個模塊都有自己的控制器、視圖、和模型,訪問的時候是按照 index.php/模塊/控制器/方法,訪問的,然后每一個控制器在 view 中對應(yīng)著一個同名的文件夾,比如說 controller/Index 控制器, view/Index 就是這個控制器對應(yīng)的模板文件夾,那么每一個方法都會在模板文件夾下對應(yīng)一個同名的 html 文件作為這個方法的模板
tp5 是通過
$this->assign('data',$data);
進(jìn)行賦值并通過
return $this->fetch('模板名');
進(jìn)行渲染的
示例代碼:
index/controller/Index.php
?php
namespace app\index\controller;
use think\Controller;
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.對 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
html>
head>
/head>
body>
hello {$name}!
/body>
/html>
如圖所示:
![](/d/20211017/6c76141c1e0eb54145dda094b50ff874.gif)
當(dāng)然在這種情況下參數(shù)名還是會很多斜杠,還是不是很友好,于是我們可以在 config.php 中將默認(rèn)的斜杠分隔符進(jìn)行修改,改成其他的這樣就避免了這個問題
4.URL 自動生成
tp5 給我們提供了 url() 這個函數(shù)幫我們自動生成 Url
public function url()
{
echo url('url2','a=1b=2');
}
這個方法運行的結(jié)果就是
/index/index/url2/a/1/b/2.html
5.請求和響應(yīng)
![](/d/20211017/bb5242a71362776d1378a487a11f8835.gif)
1.接收請求的參數(shù)
訪問: http://localhost/index/index/req/username/test
通過以下代碼可以得到 username
echo $this->request->param('username');
或者我們可以使用函數(shù)助手 input(),下面這段代碼能達(dá)到和上面一樣的效果
包括我們通過下面的代碼獲取 url
echo $this->request->url();
這個也有自己的函數(shù)助手
我們可以獲分別獲取 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() 來進(jìn)行模板渲染
return json($data);
return xml($data);
當(dāng)然我們的 tp 也有對一些東西的封裝,比如實現(xiàn)輸出一段話然后進(jìn)行跳轉(zhuǎn)到某個方法,或者是直接進(jìn)行重定向
return json($data);
return xml($data);
6.模板與輸出
一般的模板渲染就不想介紹了,這里說下模板布局,其實就是在 view 文件夾下有一個 layout.html 文件,這個文件的內(nèi)容是這樣的
layout.html
{include file="/index/header"/}
{__CONTENT__}
{include file="/index/footer"/}
然后我們寫模板的時候就在最上面加上對這個文件的引用
如果我們想全局引入頁眉頁腳,這個配置需要在 config.php 中進(jìn)行設(shè)置,在模板配置中添加下面的代碼
'layout_on' => 'true',
'layout_name' => 'layout',
'layout_item' => '{__CONTENT__}',
這樣的話就是進(jìn)行了全配置但是如果我們有些頁面不想這樣配置的話我們需要在這樣的頁面上寫上
如果我們模板文件中的靜態(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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- ThinkPHP框架搭建及常見問題(XAMPP安裝失敗、Apache/MySQL啟動失敗)
- Thinkphp搭建包括JS多語言的多語言項目實現(xiàn)方法