Understanding and deepening of relative path and absolute path

Time:2020-6-6

What is relative path and absolute path

Last week’s report solved some problems, but also exposed many problems, one of which is the relative path and absolute path. For PHP using xampp to build a server, the relative path refers to the current file relative to the user’s access, and the absolute path refers to the file relative to HtDocs.


Understanding and deepening of relative path and absolute path


Why not use absolute path

I really didn’t understand the relative path and absolute path that other members of the team said before, but I didn’t use them later, so I didn’t check them. In fact, I didn’t use them when I was supposed to. It’s also related to that I wasn’t careful before. At the beginning, I read some knowledge of the tutorial only once, but I didn’t have much impression. Then I reported a mistake when I set the template later, saying to find it Before CSS files, the files will be placed according to the path given above. No error will be reported when they are placed. They can be used normally, and they will be ignored.

Why use absolute path

During the meeting, I learned the importance of absolute path through demonstration, but at that time, I didn’t know how to deal with it
After the meeting, I think there will be such an important problem in the course, and then I turned to the course


Understanding and deepening of relative path and absolute path


Understanding and deepening of relative path and absolute path


At this time, I understand that if it is not specified, it is the CSS file found by relative path by default, so there is a problem


Understanding and deepening of relative path and absolute path


teacher_ ID is the parameter name. If relative path is used, then the subsequent CSS /… Will not be considered as CSS style any more, but as a parameter, which will cause failure of referencing CSS style.


Understanding and deepening of relative path and absolute path


Although we found the CSS style file through the address, this address is only understood as the student’s CSS style file by ourselves. In other people’s eyes, this is a CSS method under the student controller, and the subsequent error report also confirms this
Understanding and deepening of relative path and absolute path


Understanding and deepening of relative path and absolute path


Because we refer to CSS style in the V layer of student and teacher controller, CSS /… Appears from the back of the controller, so it is naturally regarded as the method under the controller
Let’s take a look at the source code


Understanding and deepening of relative path and absolute path


As you can see, when the source code is passed to the browser, it is just passed to the browser “CSS”/ bootstrap.min.css ”Instead of the exact address, the browser will look for it according to the path of thinkp5 / public / index / student / CSS. At this time, the problem is exposed. If the URL path changes, the browser will look for it according to the path again, and it will fail to find the CSS style file This problem can be avoided by using absolute path.


Understanding and deepening of relative path and absolute path


This is where the CSS files are stored


Then, the absolute path is not interfered by the address, while the relative path is interfered by the address. If you add it after public/ index.php , or delete the index after student or teacher as above, and input CSS directly, then an error will appear


Understanding and deepening of relative path and absolute path


Understanding and deepening of relative path and absolute path


Understanding and deepening of relative path and absolute path


If we use the absolute address according to the tutorial, the browser will look for the CSS style file according to the absolute address. At this time, the path change will not cause the situation that the CSS style file cannot be referenced


Understanding and deepening of relative path and absolute path


Understanding and deepening of relative path and absolute path


At the same time, even if we later delete the index.html When inputting CSS, there will be no file problem as above. At this time, a “method does not exist error” will be reported, and ambiguity will not appear as before, which will bring trouble to team cooperation.


Understanding and deepening of relative path and absolute path


Summary and reflection

It’s really frustrating to go back to the dormitory after the meeting. It’s clear that the things of the meeting are not grasped, and there are always problems in the recent report. Sometimes it’s hard to see some problems, or it’s not important to put them aside. As time goes by, it causes some big mistakes. In fact, I’m very grateful for this opportunity, which gives me a chance to reflect and let me know The importance of solving problems and applying knowledge, however, is also exposed in these problems. Otherwise, I can’t remember them in the future, and the harvest is quite great.