Home >PHP Framework >ThinkPHP >A brief analysis of how to configure the value of __public__ in thinkphp

A brief analysis of how to configure the value of __public__ in thinkphp

PHPz
PHPzOriginal
2023-04-11 10:42:421011browse

thinkphp is a modern PHP framework that is widely used in various types of web applications. During the development of web applications using thinkphp, you may encounter a variable named __public__. This variable is typically used to locate the path to public static folders, such as CSS and JavaScript files. This article will introduce how to configure the value of __public__ so that you can use the thinkphp framework more conveniently.

What is __public__?

When using the PHP framework, you may create a folder named public, which contains all public CSS, images, scripts and other static files. In the thinkphp framework, in order to avoid resource path confusion, it will rename the public folder to __public__, and let the framework recognize this folder through some simple configurations.

When you reference static files in the page, you can use __PUBLIC__ predefined constants to replace the path of the "__public__" folder. For example, if you have a file named "styles.css" in the __public__/styles folder, you can reference this style file in HTML by:

<link rel="stylesheet" type="text/css" href="__PUBLIC__/styles/styles.css">

This This allows you to locate and correct CSS and JavaScript files more easily.

How to configure the value of __public__?

If you want to modify the folder path of __public__, or set it to another name, you can modify it in the app.php configuration file. app.phpThe configuration file is the global configuration file of the thinkphp framework and is located in the /config folder in the application root directory.

Open the app.php file and you will see some configuration items. Find the following configuration item:

// +----------------------------------------------------------------------
// | 模板设置
// +----------------------------------------------------------------------

'template' => [
    // 模板后缀
    'view_suffix' => 'html',
],

'view_replace_str' => [
    '__PUBLIC__' => '/public',
    '__STATIC__' => '/public/static',
    '__MY_STATIC__' => '/my_static',
],

In the view_replace_str array, you will see that the value of __PUBLIC__ is set to /public. This value is the path to the default __public__ folder of the thinkphp framework. If you want to change the value of __public__, just modify this path.

For example, if you want to change __public__ to /my_public, you only need to change '__PUBLIC__' => '/public' Modify this line of code to '__PUBLIC__' => '/my_public'. When referencing static files in HTML tags, the framework replaces the value of the __PUBLIC__ predefined constant accordingly.

Summary

In this article, we talked about the role of __public__ in the thinkphp framework, and how to modify __public__## through the configuration file # value. With this setting, you can modify the name and path of the static resource folder according to your needs, making it easier to manage and locate these files.

When you use a framework, understanding and mastering these tips can make you develop web applications more efficiently. Happy development!

The above is the detailed content of A brief analysis of how to configure the value of __public__ in thinkphp. 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