How do you determine the paths in the application?

I use global constants, for example:

/project
    /application
        bootstrap.php
    /public
        index.php

index.php

  • defines PUBLIC_PATH and APPLICATION_PATH
  • calls APPLICATION_PATH. bootstrap.php

bootstrap.php

  • defines LIBRARY_PATH, MODULES_PATH, TEMP_PATH, CONFIG_PATH, ...
  • really works

Also I want to ask, is there a better way to do this?

+3
source share
3 answers

Do you mean that your application is not publicly available? Anyway, usually I just define a constant ROOTin my front controller (usually index.php) as follows:

define('ROOT', str_replace('\\', '/', __DIR__));

Or in older versions of PHP, where it is __DIR__not available:

define('ROOT', str_replace('\\', '/', dirname(__FILE__)));

, - :

include(ROOT . '/application/libraries/Email.php');

:

define('LIBRARY_PATH', ROOT . '/application/libraries');
include(LIBRARY_PATH . '/Email.php');

. =)

+1

:

, LOAD PHP script, index.php bootstrap.php

define("PROJECT_DISK_PATH", str_replace('\\', '/', dirname(dirname(__FILE__))) . '/');
/*
Server variables $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_FILE_NAME'] are both USELESS to 
accomplish this task because they both return the currently executed script and not this included file path.
*/

PHP script :

include(PROJECT_DISK_PATH . 'path/to/your/script/somescript.php')

, LOAD JS/CSS script :

define("PROJECT_DOCROOT_PATH", '/' . substr(PROJECT_DISK_PATH, strlen($_SERVER['DOCUMENT_ROOT'] . '/')));
define("PROJECT_HTTP_PATH", "http://" . $_SERVER['HTTP_HOST'] . JPL_DOCROOT_PATH);

, :

   <script type="text/javascript" src="<?php echo PROJECT_DOCROOT_PATH; ?>path/to/your/script/somescript.js"></script>
+1

I am going for an absolute path when it’s possible, using $_SERVER['DOCUMENT_ROOT']
When it’s not possible, I use relative paths, as Alix does.

-1
source

Source: https://habr.com/ru/post/1746361/


All Articles