为 PHP 开发提供类似 LaTeX 的 PATH 系统

发布于 2024-07-30 01:22:13 字数 949 浏览 2 评论 0原文

我在 ~/Dropbox/db/ 开发我的项目。 我需要不断地将文件夹移动到 /var/www 才能在 Firefox 中看到结果,因为这是我唯一拥有 Apache 的地方。

我的文件树位于 ~/Dropbox/db/

.
|-- handlers
|   |-- login.php
|   |-- question.php
|   |-- question_answer.php
|   `-- register.php
|-- index.php
|-- forms
|   |-- login.php
|   |-- question.php
|   |-- question_answer.php
|   `-- register.php

例如,我在文件 ~/Dropbox/db/forms/login.php 中有以下引用:

include '../handlers/login.php';

这很糟糕,因为一切都会在一段时间后崩溃。 例如,我在index.php 启动Firefox。 这意味着所有路径都相对于index.php。 这使得上面的命令毫无意义。

也许,问题的唯一解决方案是在我的所有文件中将所有路径设置为绝对路径。 我试图避免这种情况,因为这会迫使我使用 SED 的替换命令。

我想要一个与 LaTeX 的 \graphicspath 类似的用于表单和处理程序的命令,这样我就可以简单地说出在一个文件中在哪里找到给定的文件。 我发现PHP有以下函数需要位于index.php的开头。 然而,这似乎还不够,因为我无法将其扩展到表单和处理程序等。

$path_parts = pathinfo('/var/www/index.php');

如何为 PHP 建立一个类似 LaTeX 的 PATH 系统?

I develop my project at ~/Dropbox/db/. I need to move the folder continuously to /var/www to be able to see the result in Firefox because it is the only place where I have Apache.

My filetree is at ~/Dropbox/db/

.
|-- handlers
|   |-- login.php
|   |-- question.php
|   |-- question_answer.php
|   `-- register.php
|-- index.php
|-- forms
|   |-- login.php
|   |-- question.php
|   |-- question_answer.php
|   `-- register.php

I have for instance the following reference in the file ~/Dropbox/db/forms/login.php:

include '../handlers/login.php';

which is awful, since everything breaks after a while. For instance, I start Firefox at index.php. This means that all PATHS are relative to index.php. This makes the command above meaningless.

Perhaps, the only solution to the problem is to make all PATHs absolute in all my files.
I have tried to avoid this because this would force me to use SED's replacement command.

I would like to have a similar command as LaTeX's \graphicspath for forms and handlers such that I can simply say where to find the given files in one file. I found out that PHP has the following function which needs to be at the beginning of index.php. However, it does not seem to be enough, since I cannot extend it to forms and handlers, for instance.

$path_parts = pathinfo('/var/www/index.php');

How can you have a LaTeX-like PATH -system for PHP?

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

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

发布评论

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

评论(5

香草可樂 2024-08-06 01:22:13

如果您始终希望包含相对于当前执行文件当前所在的文件夹,则可以使用以下内容:

include(dirname(__FILE__) . '/../hello.php');

因此,如果该代码位于 x.php 中并且您包含 x.php 来自 y.phpy.php 位于哪个目录并不重要,因为包含始终相对于目录 x.php 已加入。

我个人认为这是实现您想要的目标的最轻松的方法。 您不需要任何特殊的配置或设置,并且所有路径都是“相对”的。

来自 PHP 文档

__FILE__:文件的完整路径和文件名。 如果在包含内部使用,则返回包含文件的填充路径和文件名。

If you always want your includes to be relative to the folder the current executing file is currently residing in, you can use the following:

include(dirname(__FILE__) . '/../hello.php');

So, if that code is in x.php and you include x.php from y.php, it doesn't matter in which directory y.php is since the include is always relative to the directory x.php is in.

I personally think it's the most effort-less way of achieving exactly what you want. You don't need any special configuration or setup, and all paths are "relative".

From the PHP Documentation

__FILE__: The full path and filename of the file. If used inside an include, the fill path and filename of the included file is returned.

缘字诀 2024-08-06 01:22:13

方法 1

include_path 是您要查找的内容吗? 您可以在 php.ini 或 apache 虚拟主机中设置它:

<VirtualHost *:80>
    ...
    php_value include_path ".:/usr/share/php:/usr/share/pear:/usr/local/share/php"
    ...
</VirtualHost>

不如 kpathsea (Τεχ 使用的)那么好,但尽可能接近。 我更喜欢这种方法,因为它可以轻松地将包含文件保留在文档根目录之外(因此您不必担心有人直接执行它们)。

方法 2

您可以使用 $_SERVER['DOCUMENT_ROOT'] 获取文档根目录。 因此您可以使用它来编写完整的包含路径。

Approach 1

Is include_path what you're looking for? You can set it either in php.ini, or in your apache vhost:

<VirtualHost *:80>
    ...
    php_value include_path ".:/usr/share/php:/usr/share/pear:/usr/local/share/php"
    ...
</VirtualHost>

Not quite as nice as kpathsea (what Τεχ uses), but as close as you can get. I prefer this approach, as it makes it easy to keep your include files outside your document root (so you don't have to worry about someone executing them directly).

Approach 2

You can get your document root using $_SERVER['DOCUMENT_ROOT']. So you can use that to write a full include path.

酒浓于脸红 2024-08-06 01:22:13

另一种方法是从 ~/Dropbox/db/ 创建到 /var/www/ 的符号链接
(不要忘记设置 htaccesss 以跟随符号链接)

Another approach would be to create a symbolink link to /var/www/ from ~/Dropbox/db/
(Don't forget to set htaccesss to follow symlinks)

栀梦 2024-08-06 01:22:13

我不知道 LaTeX 到底是做什么的,但是我不知道为什么会发生您所描述的错误,或者您是否认为会发生这种情况?
当您将firefox指向index.php时,index.php中的所有路径都将相对于index.php,但是login.php中的所有路径将(应该)相对于login.php,即使login.php包含在index.php中。
不必执行此类操作的一种方法是使用变量来存储基本路径。 但这对于您拥有的布局来说效果不佳。

另一方面,我确实有一个解决方案来解决您移动文件的问题,只需重新配置 apache
对我来说,需要编辑的文件是 /etc/apache2/sites-enabled/000-default
您需要执行

sudo gedit /etc/apache2/sites-enabled/000-default

并输入您的密码。
将路径更改为 Dropbox/db 目录的完整路径,

它最终应该是这样的,将 USERNAME 替换为您的用户名。

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /home/USERNAME/Dropbox/db
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /home/USERNAME/Dropbox/db/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order deny,allow
        deny from all
        allow from 127.0.0.1
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

这样您就不必移动文件了。

I don't know exactly what LaTeX does, however I do not know why the errors you describe are happening, or is it that you think this would happen?
When you point firefox to index.php all paths in index.php will be relative to index.php however all paths in login.php will (should) be relative to login.php , even if login.php is included by index.php.
Also one way of not having to do such things is to use a variable to store the base path. But that won't work well for the layout you have.

On the other hand I do have a solution to your problem of moving files around, just reconfigure apache
for me the file needing edit was /etc/apache2/sites-enabled/000-default
you will need to do

sudo gedit /etc/apache2/sites-enabled/000-default

and enter your password.
change the paths to the full path to the Dropbox/db directory

it should end up something like this, replace USERNAME with your username.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /home/USERNAME/Dropbox/db
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /home/USERNAME/Dropbox/db/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order deny,allow
        deny from all
        allow from 127.0.0.1
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

Then you wont have to move the files around.

盛夏已如深秋| 2024-08-06 01:22:13

一个好的解决方案是让整个程序从一个文件运行,通常是index.php。 在这种情况下,您要做的就是接受 GET 参数,该参数告诉您要执行什么操作。 这将使您可以包含始终与index.php相关的文件。

例如:index.php?action=login 或index.php?action=register。

这也使得 URL 独一无二,因此可以保留正常的浏览行为。

虽然这在最初看起来有点不寻常,但有许多 PHP 程序以这种方式运行(例如 Drupal 或 < a href="http://www.symfony-project.org/" rel="nofollow noreferrer">Symfony),它运行得很好。

A good solution is to have your entire program run from one file, normally index.php. In that case what you would do is accept a GET parameter which tells you what action to do. This would make it so that you could include files always relative to index.php.

For instance: index.php?action=login or index.php?action=register.

This also makes for unique URLs so normal browsing behavior is retained.

Although this might seem a bit unusual at forst there are a number of PHP programs which operate in this way (e.g. Drupal or Symfony) and it works very well.

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