简体   繁体   English

使用绝对或相对路径?

[英]use absolute or relative path?

in my config.php where i have all constants i set the PATH to a absolute path. 在我的config.php中,我有所有常量我将PATH设置为绝对路径。

but this means that when i move my application folder i have to change this path. 但这意味着当我移动我的应用程序文件夹时,我必须更改此路径。

i wondered if its better to set a relative path, in that way whenever i move my application between production and development folder, i dont have to change it. 我想知道是否更好地设置相对路径,这样每当我在生产和开发文件夹之间移动我的应用程序时,我都不必改变它。

how do you guys do when you move between folders? 当你在文件夹之间移动时,你们怎么做?

The best way I've found is to do the following: 我发现的最好方法是执行以下操作:

define("PATH", realpath(dirname(__FILE__)));

That gives you the directory of the current file. 这为您提供了当前文件的目录。 If you do this in your settings/bootstrap/init file, you'll have it available to your application, and it will work for any file system. 如果在settings / bootstrap / init文件中执行此操作,则可以将其提供给应用程序,它适用于任何文件系统。

__FILE__是你的朋友。

define('BASE_PATH', dirname(realpath(__FILE__)));

This will make your scripts more portable. 这将使您的脚本更具可移植性。

Include a file like this 包括这样的文件

include BASE_PATH . 'includes/header.php';

IMO, absolute paths are bad news. IMO,绝对路径是坏消息。 Even if you don't plan to move, your hosting provider could move you, like DreamHost recently did to me. 即使您不打算搬家,您的托管服务提供商也可能会让您感动,就像DreamHost最近给我做的那样。 I was fine.... But there are 14 references to "path" on their wiki: http://wiki.dreamhost.com/Server_Moves 我很好......但是在他们的wiki上有14个引用“path”: http//wiki.dreamhost.com/Server_Moves

I do three things to solve this: 我做了三件事来解决这个问题:

  1. The first is to use paths relative to the current file and include things using dirname(__FILE__). 第一种是使用相对于当前文件的路径,并使用dirname(__FILE__).包含内容dirname(__FILE__).

  2. The second is to use a loader include that all the pages load. 第二种是使用加载器包括所有页面加载。 This file has one responsibility: to find the include directory, usually via a relative call. 该文件有一个责任:通常通过相对调用来查找include目录。 So long as this relative relationship stays, it doesn't need changing. 只要这种相对关系保持不变,就不需要改变。

  3. I also like to support custom settings that belong to the installation rather than the codebase . 我还想支持属于安装而不是代码库的自定义设置。 This is done by an include mechanism and overrides a few settings that will be specific for the server the code is on. 这是通过包含机制完成的,并覆盖了一些特定于代码所在服务器的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM