Home > PHP Framework > ThinkPHP > Summary of entry-level knowledge points for ThinkPHP6.0

Summary of entry-level knowledge points for ThinkPHP6.0

WBOY
Release: 2022-07-15 20:32:39
forward
3764 people have browsed it

This article brings you relevant knowledge about thinkphp, which mainly organizes related issues related to getting started with ThinkPHP6.0. ThinkPHP is a free and open source, fast and simple object-oriented light The massive PHP development framework was born for agile WEB application development and simplified enterprise application development. Let’s take a look at it together. I hope it will be helpful to everyone.

Summary of entry-level knowledge points for ThinkPHP6.0

Recommended study: "PHP Video Tutorial"

1. Framework introduction

ThinkPHP is A free open source, fast and simple object-oriented lightweight PHP development framework was born for agile WEB application development and simplified enterprise application development. ThinkPHP has been adhering to the simple and practical design principle since its birth. While maintaining excellent performance and minimal code, it pays more attention to ease of use. Publishing under the Apache2 open source license agreement means that you can use ThinkPHP for free, and you are even allowed to publish/sell the applications you develop based on ThinkPHP as open source or commercial products.

  1. TP framework is a free open source, lightweight, simple, fast and agile PHP framework

  2. ThinkPHP6.0 requires PHP version 7.1 or above, the official recommendation is 7.3

  3. In addition to PHP7.1, you also need to enable the PDO database engine and MBstring string extension

2. Installation process

The official website does not provide software package downloads. The official recommendation is to use composer to download and update

About composer The installation and use will not be introduced

Since the default image source of composer is abroad and the link speed is slow, the installation time may be longer. We recommend using the domestic image (Alibaba Cloud), the code is as follows

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
Copy after login

If this is your first installation, or you need to install a new framework, in the command line interface, switch to the WEB directory you need to install and execute the following command

composer create-project topthink/think tp
Copy after login

The "tp" here is the root directory name of the project. You can change it arbitrarily. This is the application root directory we often mention later

If you have installed it before If so, then switch to your application root directory, and then execute the following command to update

composer update topthink/framework
Copy after login

3. Run the test

Enter the project directory on the cmd command line to run Command

php think run
Copy after login

Enter in the browser: http://127.0.0.1:8000 or http://localhost:8000 You will see the welcome interface

if 80 If the port is not occupied, you can also use it directly

php think run -p 80
Copy after login

Then you can use http://localhost/ to access directly. At this time, the browser’s default access file is : The index method of the application root directory /app/controller/index.php controller, and the actual path of http://localhost/ is http://localhost/index.php/index/index, (the specific reasons will not be detailed here. Explanation, will be discussed later)

Of course you can now configure localhost as a local domain name for access, I won’t go into details here

3.Here is tp .com as an example: Access the index operation of the index controller

http://tp.com                             Access the default entry file, default controller and default method

http://tp.com/ Index.php specifies the index.php entry file, the default method of the default controller

http://tp.com/index.php/index Specify the index.php entry file, the default method of the INDEX controller

http://tp.com/index.php/index/index Specify the index.php entry file, the index method of the index controller

4. Development Specifications

There is nothing to say about the development specifications. Direct reference to the manual official document

5. Directory structure

There is no need to talk about the directory structure for direct reference. Manual official document

 

六.开启调试模式

在项目的开发阶段,我们建议开启框架的调试模式

当调试模式开启后,会牺牲一部分执行效率,但能大大提高我们的 开发排错能力

当部署生产环境时,我们再关闭调试模式即可

默认安装的tp6.0框架默认不开启调试模式,这时我们可以在浏览器输入不存在的控制器或错误的地址进行验证

表示调试未开启

通过命令行安装的 TP6.0,会自动在根目录生成一个.example.env 文件,即环境变量示例文件,我们只要把这个文件更名为.env   (通常复制一份更名即可),   即可生效,同时开启调试模式  

//环境变量示例文件

APP_DEBUG = true

[APP]
DEFAULT_TIMEZONE = Asia/Shanghai

[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = test
USERNAME = username
PASSWORD = password
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true

[LANG]
default_lang = zh-cn
Copy after login

不难看出文件APP_DEBUG = true打开调试模式,若要关闭调试模式将true改为false,另外还有应用配置,数据库和语言设置,当然你也可以配置更多内容

现在我们在刷新刚才的页面,会提示控制器不存在,并且右下角会出现 Trace 调试小图标,说明调试开启了

开启调试模式有什么用:

a. 记录系统运行流程的执行过程  

b.   展示错误和调试信息,并开启日志记录  

c.   模版修改可以及时生效   (   不会被缓存干扰   )  

d.   启动右下角的   Trace   调试功能,更加强大  

e.   发生异常时,也会显示异常信息  

还有一种模式,就是关闭调试的时候,也可以显示简要的错误信息,打开根目录下 config 的 app.php 最后一行设置为true

'show_error_msg'   => false,
Copy after login

 

七.配置信息

配置文件有两种形式:一种是采用.env适用于本地开发,另一种就是在根目录下的config目录下,包含整个项目的配置,适用于生产环境

注意:官方明确表示.env文件部署后会被忽略,所以它仅适用于本地开发

配置优先级,如果在本地测试时   .env   优先于 config,从   config   配置中可以看出,它是先读取   .env   的,然后再默认配置一个自己的,而部署环境则忽略.env,只读取config  

开开发项目过程中,我们少不了获取配置文件中的属性值,那么我们怎么能获取到这些值呢?接下来我们以获取数据库的用户名为例

对于.env文件。

APP_DEBUG = true

[APP]
DEFAULT_TIMEZONE = Asia/Shanghai

[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = test
USERNAME = root
PASSWORD = 123456
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true

[LANG]
default_lang = zh-cn
Copy after login

比如要获取[DATABASE]下的USERNAME=root获取方式如下:                                                    

use think\facade\Env;                                                            return Env::get('database.username');                                              我们写一个getEnv发方法获取databases下的username,这个比较简单,直接获取.env文件下的,database下的username
Copy after login
<?php
namespace app\controller;

use app\BaseController;
use think\facade\Env;//引入env类

class Index extends BaseController
{

    public function getEnv(){
        return Env::get(&#39;database.username&#39;);
    }
}
Copy after login

对于config文件

<?php

return [
    // 默认使用的数据库连接配置
    &#39;default&#39;         => env('database.driver', 'mysql'),

    // 自定义时间查询规则
    'time_query_rule' => [],

    // 自动写入时间戳字段
    // true为自动识别类型 false关闭
    // 字符串则明确指定时间字段类型 支持 int timestamp datetime date
    'auto_timestamp'  => true,

    // 时间字段取出后的默认时间格式
    'datetime_format' => 'Y-m-d H:i:s',

    // 时间字段配置 配置格式:create_time,update_time
    'datetime_field'  => '',

    // 数据库连接配置信息
    'connections'     => [
        'mysql' => [
            // 数据库类型
            'type'            => env('database.type', 'mysql'),
            // 服务器地址
            'hostname'        => env('database.hostname', '127.0.0.1'),
            // 数据库名
            'database'        => env('database.database', ''),
            // 用户名
            'username'        => env('database.username', 'root123'),
            // 密码
            'password'        => env('database.password', ''),
            // 端口
            'hostport'        => env('database.hostport', '3306'),
            // 数据库连接参数
            'params'          => [],
            // 数据库编码默认采用utf8
            'charset'         => env('database.charset', 'utf8'),
            // 数据库表前缀
            'prefix'          => env('database.prefix', ''),

            // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
            'deploy'          => 0,
            // 数据库读写是否分离 主从式有效
            'rw_separate'     => false,
            // 读写分离后 主服务器数量
            'master_num'      => 1,
            // 指定从服务器序号
            'slave_no'        => '',
            // 是否严格检查字段是否存在
            'fields_strict'   => true,
            // 是否需要断线重连
            'break_reconnect' => false,
            // 监听SQL
            'trigger_sql'     => env('app_debug', true),
            // 开启字段缓存
            'fields_cache'    => false,
        ],

        // 更多的数据库配置信息
    ],
];
Copy after login

比如要获取[DATABASE]下的USERNAME=root获取方式如下:

use think\facade\Config;
return Config::get('database.connections.mysql.username');

我们写一个getConfig发方法获取databases下的username,这个就要麻烦一点,这里获取的顺序依次是:

所在配置文件——>数据库连接配置信息——>mysql数据库——>用户名

需要注意的是:由于config配置文件的优先级低于.env,所以,如果当前是处于开发环境,如下代码并不会获取到config目录下database配置文件的用户名,而是获取.env文件的用户名,所以想要获取正确配置则先关闭开发环境

<?php
namespace app\controller;

use app\BaseController;

use think\facade\Env;//引入env类

class Index extends BaseController
{

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }


    public function getEnv(){
        return Env::get('database.username');
    }


    public function getConfig(){
        return Config::get('database.connections.mysql.username');

    }
}
Copy after login

对于上边获取config配置有限读取.env文件的问题,我们可以在获取信息之前先使用has方法判断当前需要获取值是否存在两个文件再决定读取哪一个文件

<?php
namespace app\controller;
use think\facade\Config;
use app\BaseController;

use think\facade\Env;//引入env类

class Index extends BaseController
{
   



    public function getEnv(){
        return Env::get('database.username');
    }


    public function getConfig(){
        return Config::get('database.connections.mysql.username');

    }

    public function getSite(){

        echo Env::has('database.username');
        echo Config::has('database.connections.mysql.username');
    }
}
Copy after login

我们得到的值为字符串用echo输出

推荐学习:《PHP视频教程

The above is the detailed content of Summary of entry-level knowledge points for ThinkPHP6.0. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template