Home  >  Article  >  PHP Framework  >  How thinkphp refers to static resources (a brief analysis of methods)

How thinkphp refers to static resources (a brief analysis of methods)

PHPz
PHPzOriginal
2023-04-11 10:43:061071browse

With the development of Internet technology, more and more web developers are beginning to use PHP language to develop their own websites or applications. Among PHP frameworks, ThinkPHP is a very popular framework and has been widely used in many development projects.

During the development process, it is often necessary to use some static resources, such as CSS styles, JavaScript scripts, images, etc. This article will focus on how to reference static resources in the ThinkPHP framework.

1. Definition of static resources

So-called static resources generally refer to resources that do not require server-side processing, such as CSS, JS, pictures, etc. They are often placed in the static folder of the web application and are directly accessed by the browser, thus improving the access speed of the page.

In ThinkPHP, we usually place static resources in the public directory. In small and medium-sized projects, we may not need to configure a virtual host or CDN acceleration and can use this directory directly. However, in some large-scale projects, in order to improve user access speed, we also need to perform related optimizations.

2. Reference style resources

In front-end development, CSS style files are often used. We can use this style in the page, or reference the CSS style file through the page header. In ThinkPHP, we can reference CSS style resources through the following steps:

  1. Create a new CSS file and place the file in the public directory. For example, we create a new test.css file in the public directory. The following code is defined in it:
body {
    background-color: #f5f5f5;
}
  1. Reference the CSS file in the html page, the code is as follows:
<link rel="stylesheet" href="/public/test.css">

Explain the above code, where rel=" stylesheet" indicates that this is a style file, href="/public/test.css" indicates the path of the style file. In ThinkPHP, "/public" represents the public directory, ","test.css" represents the specific file name.

3. Reference JavaScript script resources

JavaScript is a must in Web development One of the languages, the ThinkPHP framework can also easily reference JavaScript script resources. Here is an example:

  1. Create a new JavaScript file and place the file in the public directory. For example, we created a new JavaScript file in the public directory. test.js file, which defines the following code:
function hello() {
    alert('Hello World!');
}
  1. Reference the JavaScript script in the html page, the code is as follows:
<script src="/public/test.js"></script>

Explain the above code , among which, src="/public/test.js" represents the path of the script file.

4. Reference image resources

In Web development, image resources are also very commonly used, such as pages The background image, LOGO, etc. in the. The following is an example:

  1. Put the image file in the public directory. For example, we created a new logo.png file in the public directory.
  2. Reference the image in the html page. The code is as follows:
<img src="/public/logo.png" alt="MyLogo">

Explain the above code. Among them, src="/public/logo.png" indicates the image file path. The alt attribute indicates when the image When it cannot be displayed, replace the text description of the image.

In summary, this year I personally think that it is not very difficult to reference static resources in the ThinkPHP framework. You only need to create a new corresponding file in the public directory, and then Just reference it in the html page. At the same time, we also need to pay attention to the use of relative paths and absolute paths, which can affect the loading speed of the page and may also affect the references of other JS or CSS files.

The above is the detailed content of How thinkphp refers to static resources (a brief analysis of methods). 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