PHP全局路径设置
我有这个设置。
root dir|
index.php
config.php
file.php |
|
|scripts|a.js
|
|account
|index.php
| |
|member |index.php
现在,我已将成员目录的index.php 包含到帐户目录的index.php 中。另外,帐户index.php 包括config.php,其中包含,
define( '路径', (__DIR__) );
现在,对于我使用的帐户index.php 中的所有内容,
require_once( PATH . '\file.php' );
并且工作正常。但是,当我尝试添加脚本 src 的路径时,例如,
<script type="text/javascript" src="<?php '.PATH.'scripts/livevalidation.js ?>"></script>
我收到错误,那么如何使用全局定义的路径将脚本文件夹中的 a.js 包含到帐户的 index.php 中。
谢谢。
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. Also , the account index.php includes the config.php which contains,
define( 'PATH', (__DIR__) );
Now , for all includes in account index.php I use,
require_once( PATH . '\file.php' );
and is working properly. But when I try to add the path for script src such as,
<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.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PHP“__DIR__”和“__FILE__”对于服务器来说是绝对的。您不需要在脚本中使用其中任何一个。
另外,你的 PHP 看起来有一些语法错误,这是正确的(尽管仍然不起作用:
The PHP "__DIR__" and "__FILE__" are absolute to the server. You shouldn't need to use either for your script.
Also, your PHP looks like it has some syntax errors, this would be correct (although still wouldn't work:
您的
script
标记的 PHP 语句中缺少print
或echo
语句。您还将串联句点放置在错误的位置。然而,最重要的是,您尝试包含的 JavaScript 不需要位于 PHP 语句中。话虽如此,最后一行应如下所示:
然而,最重要的是,我认为上面的内容不会按您的预期工作。
__DIR__
输出服务器端文件系统路径,这在通过 HTTP 导入 JavaScript 时没有意义。我建议更多的内容如下:在上面的示例中,
URL_ROOT
将指向一个绝对 URL,在该绝对 URL 下提供静态媒体(CSS、JavaScript 等)。You're missing a
print
orecho
statement in the PHP statement in yourscript
tag. 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.All that said, the final line should read like the following:
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. I'd recommend something more along the lines of the following:In the above example
URL_ROOT
would point an absolute URL under which your static media (CSS, JavaScript, and so on) is served.