且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP:ThinkPHP5.0下载安装和各种配置

更新时间:2022-09-05 10:27:01

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" => "123456@qq.com"
];

应用配置覆盖惯例配置原理

array_merge($arr1,$arr2),相同的键后面数组的值会覆盖前面的值

(3)扩展配置

conf/extra/email.php

return [
    'user' => 'xx@163.com',
    'password' => '123456'
];

数据库配置两者都可以

conf/extra/database.php

conf/database.php

(4)场景配置

办公和家里可以切换配置

conf/config.php

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

conf/index/extra/email.php

conf/admin/config.php

conf/admin/extra/email.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

conf/config.php

// 开启自动绑定模块
'auto_bind_module'=>true

访问:http://127.0.0.1:8009/api.php

-> api index index

(2)路由设置

conf/config.php

'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