ThinkPHP5 的簡單搭建和使用詳解

這篇文章主要介紹了ThinkPHP5 的簡單搭建和使用詳解,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

0X01 Thinkphp 的安裝

我這裏選擇的是使用 windows 下的 composer 進行安裝,收下首先下載 composer 這個工具,安裝完成以後進入我們想要創建項目的文件夾輸入下面的命令

composer create-project topthink/think tp5 dev-master --prefer-dist

這樣就會在當前目錄下形成一個 名爲 tp5 的文件夾,這個文件夾中存放的就是 thinkphp5 的基本的框架

0X02 重點目錄結構及文件介紹

 1.目錄結構

application : 應用目錄,我們的模型視圖控制器都會放在這個文件夾下,這是我們開發的主陣地

public : 這個是我們項目的入口文件,thinkphp 是一個單一入口的框架

thinkphp : 框架的核心目錄

2.關鍵文件

application/config.php 項目配置文件,開啓 debug 調試模式(在開發中)

application/database.php 數據庫配置文件

public/index.php 項目入口文件,定義了應用目錄的位置以及包含框架啓動文件來啓動框架

0X03 配置虛擬主機

1.httpd.conf 中判斷下面是否被註釋,如果被註釋請取消註釋

(1)Include conf/vhosts.conf (2)LoadModule vhost_alias_module modules/mod_vhost_alias.so

2.刪除 vhost.conf 中原有的默認內容,添加如下內容

<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

並在虛擬主機配置中寫上

AllowOverride All

注意:如果使用 phpstudy 的話,官方默認的 .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() 接受參數,當然也要在開始寫上命名空間

示例代碼:

<?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 V5<br/><span style="font-size:30px">十年磨一劍 - 爲API開發設計的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" rel="external nofollow" target="qiniu">七牛雲</a> 獨家贊助發佈 ]</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

結果:

2.模板和控制器的關係

每一個模塊都有自己的控制器、視圖、和模型,訪問的時候是按照 index.php/模塊/控制器/方法,訪問的,然後每一個控制器在 view 中對應着一個同名的文件夾,比如說 controller/Index 控制器, view/Index 就是這個控制器對應的模板文件夾,那麼每一個方法都會在模板文件夾下對應一個同名的 html 文件作爲這個方法的模板

tp5 是通過

$this->assign('data',$data); 

進行賦值並通過

return $this->fetch('模板名'); 

進行渲染的

示例代碼:

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 已經太多層了,這非常不利於搜索引擎的收錄,於是 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 就會轉給 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>

如圖所示:

當然在這種情況下參數名還是會很多斜槓,還是不是很友好,於是我們可以在 config.php 中將默認的斜槓分隔符進行修改,改成其他的這樣就避免了這個問題

4.URL 自動生成

tp5 給我們提供了 url() 這個函數幫我們自動生成 Url

public function url()
 {
  echo url('url2','a=1&b=2');
 }

這個方法運行的結果就是

/index/index/url2/a/1/b/2.html

5.請求和響應

1.接收請求的參數

訪問: http://localhost/index/index/req/username/test

通過以下代碼可以得到 username

echo $this->request->param('username');

或者我們可以使用函數助手 input(),下面這段代碼能達到和上面一樣的效果

echo input('username');

包括我們通過下面的代碼獲取 url

echo $this->request->url();

這個也有自己的函數助手

echo request()->url();

我們可以獲分別獲取 get post cookie file 等方式的參數

$this->request->get()
$this->request->post()
$this->request->cookie()
$this->request->file()

或者實例化一個 Request 對象,但是這種方法只能接受 url 後面是 & 連接的參數,重寫的好像不行

$Request = Request::instance()

$request->get()
$Rquest->post()
$Request->cookie()
$Request->file()

2.綁定參數

$this->request->bind('user',"hh");
 echo $this->request->user;

那麼爲什麼請求還要動態地綁定參數呢?因爲很多時候需要傳遞 session 的值,來維持會話

3.返回值

可以返回多種格式的值 比如 json xml 或者通過 $this->fetch() 來進行模板渲染

return json($data);
return xml($data);

當然我們的 tp 也有對一些東西的封裝,比如實現輸出一段話然後進行跳轉到某個方法,或者是直接進行重定向

return json($data);
return xml($data);

6.模板與輸出

一般的模板渲染就不想介紹了,這裏說下模板佈局,其實就是在 view 文件夾下有一個 layout.html 文件,這個文件的內容是這樣的

layout.html

{include file="/index/header"/}
{__CONTENT__}
{include file="/index/footer"/}

然後我們寫模板的時候就在最上面加上對這個文件的引用

{layout name="layout"/}

如果我們想全局引入頁眉頁腳,這個配置需要在 config.php 中進行設置,在模板配置中添加下面的代碼

'layout_on' => 'true',
'layout_name' => 'layout',
'layout_item' => '{__CONTENT__}',

這樣的話就是進行了全配置但是如果我們有些頁面不想這樣配置的話我們需要在這樣的頁面上寫上

{__NOLAYOUT__}

如果我們模板文件中的靜態文件路徑想要不寫死的話,我們可以在 php 文件中的 fecth 前設置字符替換

$this->view->replace(['__PUBLIC__' => '/static',]);

如果我們想每個方法都使用這個操作,我們就把上面這段代碼放到 控制器的構造函數裏面

function __construct(){
 parent::__construct();
 $this->view->replace(['__PUBLIC__' => '/static',]);
}

0X05 參考

https://www.kancloud.cn/thinkphp/thinkphp5-guide/30551

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章