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!