溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ThinkPHP5怎么安裝使用

發(fā)布時(shí)間:2021-02-18 14:01:02 來源:億速云 閱讀:148 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)ThinkPHP5怎么安裝使用的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

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è)文件夾下,這是我們開發(fā)的主陣地

public : 這個(gè)是我們項(xiàng)目的入口文件,thinkphp 是一個(gè)單一入口的框架

thinkphp : 框架的核心目錄

2.關(guān)鍵文件

application/config.php 項(xiàng)目配置文件,開啟 debug 調(diào)試模式(在開發(fā)中)

application/database.php 數(shù)據(jù)庫(kù)配置文件

public/index.php 項(xiàng)目入口文件,定義了應(yīng)用目錄的位置以及包含框架啟動(dòng)文件來啟動(dòng)框架

0X03 配置虛擬主機(jī)

1.httpd.conf 中判斷下面是否被注釋,如果被注釋請(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:\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ī)配置中寫上

AllowOverride All

注意:如果使用 phpstudy 的話,官方默認(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 基本的寫法

1.控制器的基本寫法

(1)模塊中的控制器實(shí)際上就是一個(gè)一個(gè)的類,這個(gè)類寫的時(shí)候要繼承 Controller 并且要在前面寫上命名空間

(2) thinkPHP5 使用 return 來返回一個(gè)html ,自動(dòng)渲染到頁面上

(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} h2{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div > <h2>:)</h2><p> ThinkPHP V5<br/><span >十年磨一劍 - 為API開發(fā)設(shè)計(jì)的高性能框架</span></p><span >[ V5.0 版本由 <a href="http://www.qiniu.com" rel="external nofollow" target="qiniu">七牛云</a> 獨(dú)家贊助發(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é)果:

ThinkPHP5怎么安裝使用

2.模板和控制器的關(guān)系

每一個(gè)模塊都有自己的控制器、視圖、和模型,訪問的時(shí)候是按照 index.php/模塊/控制器/方法,訪問的,然后每一個(gè)控制器在 view 中對(duì)應(yīng)著一個(gè)同名的文件夾,比如說 controller/Index 控制器, view/Index 就是這個(gè)控制器對(duì)應(yīng)的模板文件夾,那么每一個(gè)方法都會(huì)在模板文件夾下對(duì)應(yīng)一個(gè)同名的 html 文件作為這個(gè)方法的模板

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.對(duì) SEO 友好的路由

我們知道,我們的搜索引擎抓取頁面最多抓三層,但是我們剛剛寫的那種 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問 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>

如圖所示:

ThinkPHP5怎么安裝使用

當(dāng)然在這種情況下參數(shù)名還是會(huì)很多斜杠,還是不是很友好,于是我們可以在 config.php 中將默認(rèn)的斜杠分隔符進(jìn)行修改,改成其他的這樣就避免了這個(gè)問題

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)

ThinkPHP5怎么安裝使用

1.接收請(qǐng)求的參數(shù)

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

通過以下代碼可以得到 username

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

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

echo input('username');

包括我們通過下面的代碼獲取 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ù),重寫的好像不行

$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 的值,來維持會(huì)話

3.返回值

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

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

當(dāng)然我們的 tp 也有對(duì)一些東西的封裝,比如實(shí)現(xiàn)輸出一段話然后進(jìn)行跳轉(zhuǎn)到某個(gè)方法,或者是直接進(jìn)行重定向

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

6.模板與輸出

一般的模板渲染就不想介紹了,這里說下模板布局,其實(shí)就是在 view 文件夾下有一個(gè) layout.html 文件,這個(gè)文件的內(nèi)容是這樣的

layout.html

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

然后我們寫模板的時(shí)候就在最上面加上對(duì)這個(gè)文件的引用

{layout name="layout"/}

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

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

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

{__NOLAYOUT__}

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

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

如果我們想每個(gè)方法都使用這個(gè)操作,我們就把上面這段代碼放到 控制器的構(gòu)造函數(shù)里面

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

感謝各位的閱讀!關(guān)于“ThinkPHP5怎么安裝使用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI