简体   繁体   中英

use absolute or relative path?

in my config.php where i have all constants i set the PATH to a absolute 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.

__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. Even if you don't plan to move, your hosting provider could move you, like DreamHost recently did to me. I was fine.... But there are 14 references to "path" on their wiki: 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__).

  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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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