天天看點

PHP:ThinkPHP5.0下載下傳安裝和各種配置3、通路路徑4、配置5、環境變量配置6、入口檔案

1、ThinkPHP5.0簡介

composer包管理工具

ThinkPHP3.0不相容

php環境: 測試 開發 線上

url路由

請求響應

模闆視圖

MVC 一種設計典範 分離

model 模型 資料

view 視圖 界面顯示

controller 控制器 業務邏輯

2、環境安裝

php > 5.4.0 =5.6.10

php extension: pdo mbstring curl

mysql

apache nginx

內建開發環境

mac MAMP

方式一、github安裝

# 隻克隆最新代碼不克隆曆史 
cd www
git clone --depth=1 https://github.com/top-think/think.git think_php
cd think_php
git clone --depth=1 https://github.com/top-think/framework thinkphp
      

方式二、composer安裝

中文官網:docs.phpcomposer.com

cd www
composer --version
composer create-project --prefer-dist topthink/think think_composer      

方式三、thinkphp下載下傳

http://www.thinkphp.cn/

下載下傳完整版,解壓

方式四、github壓縮包下載下傳

think.zip -> think_php

framework.zip -> thinkphp

修改名稱

成功标志:浏覽器進入publish目錄又顯示

目錄介紹

在沒有安裝php-fpm的時候啟動

php -S localhost:8888 router.php      

子產品化設計

application: 應用目錄; 整個應用所有的内容都寫在這個目錄當中

命名規範

類 ,屬性,方法,常量,函數,變量,資料庫

3、通路路徑

子產品-控制器-方法

通用的控制可以寫在common中,它不允許url直接通路

編寫公共子產品

namespace app\common\controller;

class User
{
    public function showName($name="")
    {
        return "$name";
    }

}      

使用公共子產品的函數

namespace app\admin\controller;

use app\common\controller\User as commonUser;

class Index extends commonUser
{
    public function index()
    {
        return $this->showName("admin name");
    }
}
      

通路

http://127.0.0.1:8009/admin/index/index

4、配置

(1)慣例配置

慣例配置: thinkphp/convention.php

可用config()函數檢視

public function config()
    {
        return dump(config());
    }      

(2)應用配置

修改application -> app

public/index.php定義配置檔案路徑

// 定義配置目錄
define('CONF_PATH', __DIR__ . '/../conf/');
      

app同級目錄配置檔案

conf/config.php

return [
    "user"  => "Peng shiyu",
    "email" => "[email protected]"
];
      

應用配置覆寫慣例配置原理

array_merge($arr1,$arr2)

,相同的鍵後面數組的值會覆寫前面的值

(3)擴充配置

conf/extra/email.php

return [
    'user' => '[email protected]',
    'password' => '123456'
];      

資料庫配置兩者都可以

conf/extra/database.php

conf/database.php

(4)場景配置

辦公和家裡可以切換配置

return [
    'app_status' => 'home'
];      

conf/home.php

return [
    'address' => 'home'
];      

conf/office.php

return [
    'address' => 'office'
];      

conf/extra/database.php > conf/database.php > thinkphp/convention.php

(5)子產品配置

目錄決定了配置作用域

conf/index/config.php

conf/index/extra/email.php

conf/admin/config.php

conf/admin/extra/email.php

(6)動态配置conf/index/config.php

(6)動态配置設定config(key, value)

1、構造函數__construct()中設定整個類生效

2、目前方法配置,目前方法生效

namespace app\index\controller;

class Index
{
    public function __construct()
    {
        config('key', 'value');
    }

    public function index()
    {
        return dump(config());
    }

    public function config()
    {
        config('key', 'value');
        return dump(config());
    }
      

(7)Config類和config助手函數

thinkphp/library/think/Config.php

// 擷取全部
config() 等價于  Config::get()

// 擷取單個
config("key") 等價于  Config::get("key")

// 設定
config("key", "value") 等價于  Config::set("key", "value")

// 注意作用域
設定 config("key", "value", "index")  
擷取 config("key", "index") 

// 檢查存在
bool = Config::has("key") // 不存在和null都是false
config("?key")      

5、環境變量配置

.env配置

key=value

[databse]
hostname=localhost

database_hostname=localhost      

擷取方式一:

$_ENV['PHP_KEY']      

擷取方式二:

use think\Env;

Env::get('key', 'default');
Env::get('database_hostname', 'default');
Env::get('database.hostname', 'default');      

環境配置和場景配置

.env

app_status=dev      

config.php

use think\Env;

return [
    'app_status'=> Env::get('app_status', 'dev')
];      

dev.php

return [
    'app_now_status'=> 'dev'
];      

test.php

return [
    'app_now_status'=> 'test'
];
      

問題

列印環境變量空白

dump($_ENV);

檢視環境變量:

dump(phpInfo());

查找php.ini 檔案

修改php.ini

variables_order = "GPCS"
修改為: variables_order = "EGPCS"      

或者

;Default Value: "EGPCS"
改為
Default Value: "EGPCS"      

關閉:killall php-fpm

啟動:php-fpm

指定ini檔案啟動 php-fpm -c /private/etc/php.ini

6、入口檔案

單入口檔案

-安全檢測

-請求過濾

(1)入口檔案綁定

3位

public/index.php

// 自定義綁定 子產品/控制器
define('BIND_MODULE', 'admin/index');      

通路:

http://127.0.0.1:8009/

-> admin index index

// 開啟自動綁定子產品
'auto_bind_module'=>true      
http://127.0.0.1:8009/api.php

-> api index index

(2)路由設定

'url_route_must'         => false,
'url_route_on'           => true,      

conf/route.php

return [
    'post/:id' => 'index/index/info'
];      

app/index/controller/Index.php

class Index
{
    public function index()
    {
        return 'index index index';
    }

    public function info($id)
    {
        echo url('index/index/info', ['id'=>$id]);
        return "api index info $id";
    }
}      
http://127.0.0.1:8009/post/5

傳回:/post/5.htmlapi index info 5