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:
body { background-color: #f5f5f5; }
<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:
function hello() { alert('Hello World!'); }
<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:
<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!