开发者

use absolute or relative path?

开发者 https://www.devze.com 2022-12-25 23:19 出处:网络
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.

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 f开发者_开发百科older, 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__ is your friend.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号