如何定义应用程序中的路径?

发布于 2024-09-02 09:22:12 字数 385 浏览 6 评论 0原文

我使用全局常量,如下所示:

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

index.php

  • 定义 PUBLIC_PATH 和 APPLICATION_PATH
  • 调用 APPLICATION_PATH 。 bootstrap.php

bootstrap.php

  • 定义了LIBRARY_PATH,MODULES_PATH,TEMP_PATH,CONFIG_PATH,...
  • 确实有效

另外我想问是否有更好的方法来做到这一点?

I'm using global constants, like this:

/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, ...
  • does real work

Also i want to ask if there is better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜雨飘雪 2024-09-09 09:22:12

你的意思是你的申请不公开?无论如何,通常我只是在前端控制器(通常是 index.php)中定义一个 ROOT 常量,如下所示:

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

或者在旧版本的 PHP 上,其中 __DIR__ 不可用:

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

由于内部结构永远不会改变,我只是做类似的事情:

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

而不是:

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

减少污染。 =)

You mean your application is not public? Anyway, normally I just define a ROOT constant in my front controller (usually index.php) like this:

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

Or on older versions of PHP where __DIR__ is not available:

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

Since the inner structure never changes I just do something like:

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

Instead of:

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

Less pollution. =)

天暗了我发光 2024-09-09 09:22:12

根据您的目录树:

这是我用来加载 PHP 脚本的脚本,基本上您可以将其放在 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 脚本中执行以下操作:

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

这些是我用来加载的脚本页面中的 JS/CSS 脚本:

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>

According to your directory tree:

This is the one I would use to LOAD PHP script, basically you can place it in index.php or in 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.
*/

Then in your PHP script you do:

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

And these are the ones I would use to LOAD JS/CSS script IN PAGES:

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

So in your page you can do:

   <script type="text/javascript" src="<?php echo PROJECT_DOCROOT_PATH; ?>path/to/your/script/somescript.js"></script>
戏剧牡丹亭 2024-09-09 09:22:12

如果可能的话,我会使用 $_SERVER['DOCUMENT_ROOT']
当不可能时,我会使用相对路径,就像 Alix 所做的那样。

I am going for absolute path when possible, using $_SERVER['DOCUMENT_ROOT']
When impossible, I use relative paths, much like as Alix does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文