Home PHP Framework ThinkPHP Using static file server in ThinkPHP6

Using static file server in ThinkPHP6

Jun 20, 2023 pm 02:06 PM
thinkphp server static files

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;
}
Copy after login

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/',
Copy after login

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/
Copy after login

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',
Copy after login

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
Copy after login

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
Copy after login

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 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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem that eMule search cannot connect to the server How to solve the problem that eMule search cannot connect to the server Jan 25, 2024 pm 02:45 PM

How to solve the problem that eMule search cannot connect to the server

Detailed explanation of CentOS installation fuse and CentOS installation server Detailed explanation of CentOS installation fuse and CentOS installation server Feb 13, 2024 pm 08:40 PM

Detailed explanation of CentOS installation fuse and CentOS installation server

Solution to the inability to connect to the RPC server and the inability to enter the desktop Solution to the inability to connect to the RPC server and the inability to enter the desktop Feb 18, 2024 am 10:34 AM

Solution to the inability to connect to the RPC server and the inability to enter the desktop

Best Practice Guide for Building IP Proxy Servers with PHP Best Practice Guide for Building IP Proxy Servers with PHP Mar 11, 2024 am 08:36 AM

Best Practice Guide for Building IP Proxy Servers with PHP

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

How to run thinkphp project

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

There are several versions of thinkphp

How to configure Dnsmasq as a DHCP relay server How to configure Dnsmasq as a DHCP relay server Mar 21, 2024 am 08:50 AM

How to configure Dnsmasq as a DHCP relay server

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

How to run thinkphp

See all articles