search
HomePHP FrameworkThinkPHPUsing static file server in ThinkPHP6

With the rapid development of the Internet, static resource servers have become an important part of web application development. The static resource server is mainly responsible for the storage and distribution of static resources (such as pictures, js, css and other files), providing users with a faster and more stable access experience. In PHP development, ThinkPHP6, as a popular MVC framework, provides some built-in functions to help us quickly build a static resource server.

Thinking about static resources in ThinkPHP

In ThinkPHP6, we can easily handle requests for static resources through some built-in functions and classes. ThinkPHP can handle static resource requests under /public/ by default, so requests for directories such as /static/, /images/, /css/, and /js can be processed directly. In the /public/index.php file, the core files of ThinkPHP are introduced, and the processing logic of static resources is defined:

if (is_file(__DIR__ . '/../public' . $_SERVER['REQUEST_URI'])) {
    return false;
}

If the requested URL is a file, and this file is located in the /public/ directory , then this file will be returned directly without routing analysis and controller processing. This is because resources in the /public/ directory can be accessed directly without using routing rules to resolve them. Of course, if you need to customize the path of the /public/ directory, you can modify the public_root variable in the config/app.php file. For example:

'public_root' => __DIR__ . '/../assets/',

This will specify the assets directory of the application root directory as the root directory of the /public/ directory, instead of using the default /public/ directory.

How to use CDN to access static files

In the actual deployment environment, in order to better optimize traffic and access speed, we usually upload static files to CDN (Content Delivery Network, content distribution network ) provider for storage and distribution. Simply put, CDN is a network that uses multiple nodes to cache and distribute static resources. When users access resources, they can obtain resources from the node server closest to them, thereby providing a faster and more stable user experience. In ThinkPHP6, to use CDN to access static files, you need to make relevant configurations in the configuration file.

First you need to modify the host name of the CDN, for example, change the original /public/static resource path to:

http://cdn.example.com/static/

Then, in the config/app.php file, find app.url_html_suffix and For the two variables app.static_domain, change their values ​​to:

'url_html_suffix' => '.html',
'static_domain' => 'http://cdn.example.com',

In this way, the CDN host name and static domain name are configured, and .html is used as the pseudo-static suffix. When the static resource request arrives, The framework will match based on the URL prefix of the static_domain parameter and directly return the corresponding file on the CDN.

If you need CDN access to the files in the assets directory, you can use the following link when accessing:

http://cdn.example.com/assets/images/logo.jpg

In this case, the static files will pass the CDN name cdn.example.com for a visit. In some CDN providers, you can also specify the access method by adjusting the HTTP response header, for example:

Cache-Control: max-age=31536000,public

This response header tells the browser that this file can be cached and makes it valid within one hour.

Summary

Static resource server is an indispensable part of web application development. Especially when the number of visits is relatively large, it is very necessary to use CDN for access. In ThinkPHP6, we can easily configure the host name and static domain name of the CDN, and use some simple functions and classes to handle requests for static resources, thereby improving user access speed and experience.

The above is the detailed content of Using static file server in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools