PHP 中强大的目录管理器

发布于 2024-08-26 17:43:58 字数 60 浏览 3 评论 0原文

这是一个一般性问题。每次移动文件或将文件移动到不同目录时,您采用哪些技术来防止 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

儭儭莪哋寶赑 2024-09-02 17:43:58

对于前端内容,请始终使用绝对 URL(以“/”开头,以便您可以根据需要从一个域移动到另一个域)。

对于内部 include() / require() 类型的内容,请按照 Gaurav 的建议进行操作,并使用一个配置文件来创建一个常量来表示您的路径(这样您就可以轻松更改它)根据需要从代码中的一个位置进行)。

对于您想要重用的库类型内容(即类、函数等),我会通过 php.ini(全局或本地)、.htaccess(如果您使用apache) 或通过 ini_set() 函数。这样您就可以仅通过文件名包含这些文件(即

如果您选择 ini_set 路线,请查看 auto_append指令(反过来可以通过 php.ini、.htaccess 或 ini_set 设置)...这样您就可以将“引导”文件添加到每个页面请求中,为该应用程序设置 include_path,而无需添加ini_set 语句随处可见。

综上所述,我建议您:

  • 提前考虑您的应用程序布局,制定通用约定,并坚持下去。
  • 考虑学习设计模式(MVC 等),这将使您以新的方式思考如何设计应用程序,
  • 采用应用程序框架(CakePHP、Zend Framework 等),该框架将附带建议(或强制) )文件/目录布局,使您无需管理文件位置和内容。

祝你好运!

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 the ini_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:

  • think your application layout ahead in advance, develop a common convention, and stick to it.
  • consider learning about design patterns (MVC, et al), which will get you thinking in new ways about how you design your applications
  • adopt the use of an application framework (CakePHP, Zend Framework, etc) which will come with a suggested (or mandated) file/directory layout and keep you from having to manage file locations and stuff.

Good luck!

有深☉意 2024-09-02 17:43:58

如果您移动/重命名整个网页中链接的文件,并且希望确保链接仍然有效,我会编写 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).

握住你手 2024-09-02 17:43:58

我使用一个配置文件,其中定义了各种目录(例如“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文