PHP 中强大的目录管理器
这是一个一般性问题。每次移动文件或将文件移动到不同目录时,您采用哪些技术来防止 PHP 代码中的链接中断?
This is a general question. What are some techniques you employ to prevent the links in your PHP code from breaking every time you move a file or move a file into a different directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于前端内容,请始终使用绝对 URL(以“/”开头,以便您可以根据需要从一个域移动到另一个域)。
对于内部
include()
/require()
类型的内容,请按照 Gaurav 的建议进行操作,并使用一个配置文件来创建一个常量来表示您的路径(这样您就可以轻松更改它)根据需要从代码中的一个位置进行)。对于您想要重用的库类型内容(即类、函数等),我会通过 php.ini(全局或本地)、.htaccess(如果您使用apache) 或通过 ini_set() 函数。这样您就可以仅通过文件名包含这些文件(即
)
如果您选择 ini_set 路线,请查看 auto_append指令(反过来可以通过 php.ini、.htaccess 或 ini_set 设置)...这样您就可以将“引导”文件添加到每个页面请求中,为该应用程序设置 include_path,而无需添加ini_set 语句随处可见。
综上所述,我建议您:
祝你好运!
For front end stuff, always use absolute URLs (start with '/' so you can move from domain to domain as needed).
For internal
include()
/require()
type stuff, do as Gaurav suggests and use a config file that creates a constant to represent your path (so you can change it easily as needed from one location in your code).For library type stuff (i.e. classes, functions, etc) that you want to reuse, I would add that to the
include_path
either via php.ini (global or local), .htaccess (if you are using apache) or via theini_set()
function. That way you can include these files by just the filename (i.e.<?php include_once('library.php'); ?>
)If you go the ini_set route, take a look at the auto_append directive (which in turn can be set via php.ini, .htaccess or ini_set)... that way you can have a 'bootstrap' file added to every page request that sets up your include_path for just that application without having to add the ini_set statement every where you turn.
With all that said, I recommend that you:
Good luck!
如果您移动/重命名整个网页中链接的文件,并且希望确保链接仍然有效,我会编写 303 重定向(位置和方式取决于您的 Web 服务器及其配置)。
If you move/rename a file that is linked throughout your web page and would like to ensure that links still work, I would write a 303 redirect (where and how depends on your web server and it's configuration).
我使用一个配置文件,其中定义了各种目录(例如“webroot”、“application”或“css”)的所有路径(
using Define()
),这样我只需更改其中的一行一个文件,并且更改会影响使用该变量的所有文件。
I use a configuration file wherein I define all the paths (
using define()
) to various directories like 'webroot', 'application', or 'css'This way I need to change only one line in one file and the changes are affected in all files wherein this variable is used.