简体   繁体   English

PHP全局路径设置

[英]PHP global path setting

I have this setting. 我有这个设置。

 root dir| 
       index.php
       config.php
       file.php  |
                 |
                 |scripts|a.js
                 |
                 |account
                         |index.php
                         |         |
                         |member   |index.php
  • Now, I've included index.php of member dir into index.php of account dir. 现在,我已将成员dir的index.php包含到account dir的index.php中。 Also , the account index.php includes the config.php which contains, 此外,帐户index.php包含config.php,其中包含,

     define( 'PATH', (__DIR__) ); 

Now , for all includes in account index.php I use, 现在,对于我使用的帐户index.php中的所有包含,

require_once( PATH . '\file.php' ); 

and is working properly. 并且工作正常。 But when I try to add the path for script src such as, 但是当我尝试为脚本src添加路径时,

<script type="text/javascript" src="<?php '.PATH.'scripts/livevalidation.js ?>"></script>

I get an error, so how can i include the a.js in scripts folder into index.php of account using the globally defined path. 我收到一个错误,所以如何使用全局定义的路径将脚本文件夹中的a.js包含到account.php中。

Thanks. 谢谢。

The PHP "__DIR__" and "__FILE__" are absolute to the server. PHP“__DIR__”和“__FILE__”对于服务器来说是绝对的。 You shouldn't need to use either for your script. 您不应该使用任何一个脚本。

<script src="/scripts/livevalidation.js"></script>

Also, your PHP looks like it has some syntax errors, this would be correct (although still wouldn't work: 此外,您的PHP看起来有一些语法错误,这是正确的(虽然仍然无法正常工作:

<script src="<?php echo PATH.'/scripts/livevalidation.js'; ?>"></script>

You're missing a print or echo statement in the PHP statement in your script tag. 您在script标记的PHP语句中缺少printecho语句。 You're also placing the concatination periods in the wrong place. 你也把连接期放在错误的地方。 On top of that, however, the JavaScript you're trying to include doesn't need to be in the PHP statement. 但是,最重要的是,您尝试包含的JavaScript不需要在PHP语句中。

All that said, the final line should read like the following: 总而言之,最后一行应如下所示:

<script type="text/javascript" src="<?php echo PATH ?>scripts/livevalidation.js"></script>

On top of that, however, I don't think the above will work as you expect. 然而,最重要的是,我不认为上述内容会像您期望的那样起作用。 __DIR__ outputs a server-side filesystem path, which wouldn't make sense when importing JavaScript over HTTP. __DIR__输出服务器端文件系统路径,这在通过HTTP导入JavaScript时没有意义。 I'd recommend something more along the lines of the following: 我建议更多的内容如下:

<?php define('URL_ROOT', '/'); ?>

<script type="text/javascript" src="<?php echo URL_ROOT ?>scripts/livevalidation.js"></script>

In the above example URL_ROOT would point an absolute URL under which your static media (CSS, JavaScript, and so on) is served. 在上面的示例中, URL_ROOT将指向一个绝对URL,在该URL下提供静态媒体(CSS,JavaScript等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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