Absolute vs. relative paths

后端 未结 5 836
温柔的废话
温柔的废话 2020-11-30 03:02

If I use absolute paths, I can\'t move the whole directory to a new location. If I use relative paths, I can\'t move individual files to new locations.

What\'s the s

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 03:05

    you should use a config file that will be included in each file first line, for example your app look like this

    root / App / Plugins

    inside your root dir : app-config.php

    if ( !defined('ABSPATH') )
        define('ABSPATH', dirname(__FILE__) . '/');
    

    now, suppose you have to include a plugin file, so

    inside your Plugin dir : my-plugin.php

    require_once '../../app-config.php';
    

    now everything below this line can use ABSPATH

    example do you want to load an image

    
    

    now, the thing is more simple if your app is designed to automatically load some files like

    plugin-widget-1.php
    

    so that everything inside this file or any other file loaded by the my-plugin.php file can use the ABSPATH without include each time the app-config.php file.

    with this in mind you can have all the short-hand you want into the app-config.php example

    define('UPLOAD_PATH', ABSPATH. 'Public/uploads/');
    define('IMAGES_PATH', ABSPATH. 'Public/images/');
    define('HELPERS_PATH', ABSPATH. 'App/helpers/');
    ...
    

    so, now that you have all defined, if you need to move a file, let's say one folder forward example:

    root / App / Plugins / Utils

    just inlucde require_once '../../../app-config.php';

    obviously i suppose that you are not changing paths each time =) anyway if you need to do so is always more simple to change one file inclusion instead of hundreds.

    hope this make sense to you =)

提交回复
热议问题