【ThinkPHP】ThinkPHP環境的安裝與配置


ThinkPHP是一個免費開源的,快速、簡單的面向對象的輕量級PHP開發框架。

嚴格來說,ThinkPHP無需安裝過程,這里所說的安裝其實就是把ThinkPHP框架放入WEB運行環境(前提是你的WEB運行環境已經OK),可以通過兩種方式獲取和安裝ThinkPHP。

下載ThinkPHP安裝

獲取ThinkPHP的方式很多,官方網站(http://thinkphp.cn)是最好的下載和文檔獲取來源。
官網提供了穩定版本的下載:http://thinkphp.cn/down/framework.html
由於ThinkPHP5.0還在測試階段,所以需要通過Git服務器下載,Git服務地址:https://github.com/top-think/think

下載或者使用GIT克隆到本地后,請(解壓縮后)放置於你的WEB根目錄下面的tp5子目錄。

還有一種方式,就是通過Composer進行安裝,這里不再介紹了,傳送門:https://www.kancloud.cn/thinkphp/thinkphp5-guide/30549

無論你采用什么方式獲取的ThinkPHP框架,現在只需要做最后一步來驗證是否正常運行。

在瀏覽器中輸入地址:
http://localhost/tp5/public/
如果瀏覽器輸出如圖所示:

現在已經完成了ThinkPHP的安裝!

 

接下來進一步分析一下ThinkPHP5.0的框架結構:

首先是瀏覽一下ThinkPHP5.0的目錄結構

tp5  WEB部署目錄(或者子目錄)
├─application           應用目錄
│  ├─common             公共模塊目錄(可以更改)
│  ├─module_name        模塊目錄
│  │  ├─config.php      模塊配置文件
│  │  ├─common.php      模塊函數文件
│  │  ├─controller      控制器目錄
│  │  ├─model           模型目錄
│  │  ├─view            視圖目錄
│  │  └─ ...            更多類庫目錄
│  │
│  ├─command.php        命令行工具配置文件
│  ├─common.php         公共函數文件
│  ├─config.php         公共配置文件
│  ├─route.php          路由配置文件
│  ├─tags.php           應用行為擴展定義文件
│  └─database.php       數據庫配置文件

├─public                WEB目錄(對外訪問目錄)
│  ├─index.php          入口文件
│  ├─router.php         快速測試文件
│  └─.htaccess          用於apache的重寫

├─thinkphp              框架系統目錄
│  ├─lang               語言文件目錄
│  ├─library            框架類庫目錄
│  │  ├─think           Think類庫包目錄
│  │  └─traits          系統Trait目錄
│  │
│  ├─tpl                系統模板目錄
│  ├─base.php           基礎定義文件
│  ├─console.php        控制台入口文件
│  ├─convention.php     框架慣例配置文件
│  ├─helper.php         助手函數文件
│  ├─phpunit.xml        phpunit配置文件
│  └─start.php          框架入口文件

├─extend                擴展類庫目錄
├─runtime               應用的運行時目錄(可寫,可定制)
├─vendor                第三方類庫目錄(Composer依賴庫)
├─build.php             自動生成定義文件(參考)
├─composer.json         composer 定義文件
├─LICENSE.txt           授權說明文件
├─README.md             README 文件
├─think                 命令行入口文件
~~~

其中入口文件就是 tp5\public\index.php,上面使用的http://localhost/tp5/public/地址,最開始加載的文件就是tp5\public\index.php文件,因為index.php是默認被加載的,所以完整的路徑是http://localhost/tp5/public/index.php

上面的應用(application)目錄,就是我們寫邏輯代碼的地方。

ThinkPHP5.0是基於MVC(模型-視圖-控制器)的方式來組織的。

ThinkPHP5.0的URL訪問受路由決定,如果關閉路由或者沒有匹配路由的情況下,則是基於:

http://serverName/index.php(或者其它應用入口文件)/模塊/控制器/操作/參數/值…


然后我們來看看實際的文件:

這是ThinkPHP5.0的默認目錄文件,我們可以看到模塊為index,控制器也是index,操作也是index。

模塊文件夾下面除了有controller文件夾外,還可以新增model文件夾(模型),view文件夾(視圖),這些文件夾就是ThinkPHP5中MVC架構的體現。

 

知道這些后,再結合上面URL的完整路徑規則,那么可以知道訪問“index操作”的完整路徑是:

http://localhost/tp5/public/index.php/index/index/index

其中最右邊的index代表操作,右邊第二個index代表控制器,右邊第三個index代碼模塊。

接下來筆者用一個登錄功能來串聯一下這些知識點:
項目結構

數據庫


index.php文件

<?php
namespace app\index\controller;
use app\index\model\User;
use think\Db;

class Index
{
    public function index()
    {
        return 'hello thinkphp5';
    }
    
    public function loginview(){
        return view();
    }
    
    public function login(){
        $name = $_GET["name"];
        $pass = $_GET["password"];
        
        $user = new User($name,$pass);
        
        //查詢數據庫
        $val = Db::table('userinfo')
            ->where('name',$user->GetName())
            ->where('pass',$user->GetPass())
            ->find();
        
        $res = "";
        if($val){//有該用戶
            $_SESSION["user"] = $user->GetName();
            $res = "login successed";
        }else{//沒有該用戶
            $res = "login failed";
        }
        return $res;
    }
}
index.php

User.php文件

<?php
namespace app\index\model;
class User{
    private $name;
    private $pass;
    
    public function __construct($name,$pass){
        $this->name = $name;
        $this->pass = $pass;
    }
    
    public function GetName(){
        return $this->name;
    }
    
    public function GetPass(){
        return $this->pass;
    }
}
User.php

loginview.html文件

<form action = "login">

<div><span>用戶名:</span><input type="text" name="name" /></div>

<div><span>密碼:</span><input type="password" name="password"/></div>

<div><input type="submit" value="登錄" /></div>
</form>
loginview.html

database.php文件

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

return [
    // 數據庫類型
    'type'            => 'mysql',
    // 服務器地址
    'hostname'        => '127.0.0.1',
    // 數據庫名
    'database'        => 'test',
    // 用戶名
    'username'        => 'root',
    // 密碼
    'password'        => '',
    // 端口
    'hostport'        => '3306',
    // 連接dsn
    'dsn'             => '',
    // 數據庫連接參數
    'params'          => [],
    // 數據庫編碼默認采用utf8
    'charset'         => 'utf8',
    // 數據庫表前綴
    'prefix'          => '',
    // 數據庫調試模式
    'debug'           => true,
    // 數據庫部署方式:0 集中式(單一服務器),1 分布式(主從服務器)
    'deploy'          => 0,
    // 數據庫讀寫是否分離 主從式有效
    'rw_separate'     => false,
    // 讀寫分離后 主服務器數量
    'master_num'      => 1,
    // 指定從服務器序號
    'slave_no'        => '',
    // 是否嚴格檢查字段是否存在
    'fields_strict'   => true,
    // 數據集返回類型
    'resultset_type'  => 'array',
    // 自動寫入時間戳字段
    'auto_timestamp'  => false,
    // 時間字段取出后的默認時間格式
    'datetime_format' => 'Y-m-d H:i:s',
    // 是否需要進行SQL性能分析
    'sql_explain'     => false,
];
database.php

然后使用 http://localhost/tp5/public/index.php/index/index/loginview 訪問登錄

效果圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM