简体   繁体   English

如何为不同的部署环境更新 PHP 文件路径

[英]How to update PHP File paths for different deployment environments

I am looking to update file paths of model/controllers or for that matter even basic about.php to something without the .php extension like just about/, in the source-code, based on the environments.我希望将模型/控制器的文件路径或就此而言,甚至是基本的 about.php 更新为没有 .php 扩展名的内容,例如源代码中的 about/,基于环境。

Why?为什么? Don't want to reveal the actual paths and variables for security.为了安全起见,不想透露实际路径和变量。

For Example:例如:

When I call a delete_user controller in the DEV environment, currently the href in <a> tag is pointing to delete_user.php passing the User-ID as a GET variable.当我在 DEV 环境中调用 delete_user 控制器时,当前<a>标记中的href指向delete_user.php ,将用户 ID 作为 GET 变量传递。 Then there is a controller which runs the DELETE SQL Query, and redirects back to the calling page.然后有一个控制器运行 DELETE SQL 查询,并重定向回调用页面。

<a href="delete_user.php?delete_user_id=<?= $row['user_id'] ?>" class="table-action table-action-delete" data-toggle="tooltip" data-original-title="Delete">
</a>

On Production Environment, I want the href to be instead as delete-user/user-id .在生产环境中,我希望将href改为delete-user/user-id

Now, the href file-paths should be updated and interpreted seamlessly across environments.现在,应该跨环境无缝更新和解释 href 文件路径。

<a href="delete-user/<?= $row['user_id'] ?>" class="table-action table-action-delete" data-toggle="tooltip" data-original-title="Delete">
</a>

Environment: NGINX, PHP7.4 (LEMP)环境:NGINX、PHP7.4 (LEMP)

What is the best and scalable way to achieve this in PHP?在 PHP 中实现这一目标的最佳且可扩展的方法是什么?

You can add environment variables in your php-fmp settings and read those in your code.您可以在 php-fmp 设置中添加环境变量并在代码中读取这些变量。 This will depend on your server setup, so you can google what you need.这将取决于您的服务器设置,因此您可以通过谷歌搜索您需要的内容。

https://www.digitalocean.com/community/questions/setting-environment-variables-in-php-fpm https://www.digitalocean.com/community/questions/setting-environment-variables-in-php-fpm

Then in your php you have access to those variables either using $_ENV or getenv()然后在您的 php 中,您可以使用$_ENVgetenv()访问这些变量

https://www.php.net/manual/en/reserved.variables.environment.php https://www.php.net/manual/en/reserved.variables.environment.php

https://www.php.net/manual/en/function.getenv.php https://www.php.net/manual/en/function.getenv.php

alternatively, there may be some field of the $_SERVER variable that can do what you need, but these will be specific to your setup so you need to var_dump($_SERVER) in your dev environment and in your prod environment and find a field that can work for you.或者,可能有$_SERVER变量的某些字段$_SERVER您的需要,但这些字段将特定于您的设置,因此您需要在开发环境和生产环境中使用var_dump($_SERVER)并找到一个字段可以为你工作。

https://www.php.net/manual/en/reserved.variables.server.php https://www.php.net/manual/en/reserved.variables.server.php

If I were you, I will consider three ways :如果我是你,我会考虑三种方式:

The first one is involving that you can maintain a custom entity called ApplicationLinksConfiguration map for your various env.第一个涉及您可以为各种环境维护一个名为 ApplicationLinksConfiguration 映射的自定义实体。

Properties :特性 :

  • $configKey : universal key as text or integer, as you prefer $configKey : 通用键作为文本或整数,如你$configKey
  • $testValue : value for the test step env $testValue : 测试步骤 env 的值
  • $stagingValue : value for the staging step env $stagingValue : 暂存步骤 env 的值
  • $prodValue : value for the prod final step env $prodValue : prod 最后一步 env 的值

And any other steps you need.以及您需要的任何其他步骤。

Of course you can add setters and getters and a custom constructor to build an object.当然,您可以添加 setter 和 getter 以及自定义构造函数来构建对象。

Of course you can add a factory system or any builder design pattern to simplify your links management as your config system.当然,您可以添加工厂系统或任何构建器设计模式,以将链接管理简化为配置系统。

The second way to consider is that you can use your framework links builder system, if any.要考虑的第二种方法是您可以使用您的框架链接构建器系统(如果有)。 As an example, when you are using Zend Framework , or Laminas which is the last version of that same framework, there are some view helpers which are responsible to build your links through all your webapplication.举个例子,当您使用Zend Framework ,或Laminas这是同一个框架的最后一个版本,有一些视图助手分别负责建立,通过所有的web应用的链接。

There are of course many view helpers , but the url view helper can show you many advantages in a daily work basis.视图助手当然有很多,但是url 视图助手可以在日常工作中向您展示许多优点。 For static resources, there is the asset view helper too.对于静态资源,也有资产视图助手

So, as there are different application statuses managed inside that framework, you can implement yours following the directives specified inside the official documentation, especially this section and the next one .因此,由于该框架内管理着不同的应用程序状态,您可以按照官方文档中指定的指令来实现您的应用程序状态,尤其是本节和下一节

Now you can build a custom ApplicationLinksConfiguration manager who is application mode aware, and write custom env with your custom rules to build your links like that : one env, one custom rule.现在,您可以构建一个自定义ApplicationLinksConfiguration管理器,它可以识别ApplicationLinksConfiguration程序模式,并使用自定义规则编写自定义 env 来构建这样的链接:一个环境,一个自定义规则。 Or one ring for hrefs, one ring to build all your your hrefs.或者一环用于hrefs,一环用于构建您的所有hrefs。

And the last way is to define custom routes for specific env and/or custom needs.最后一种方法是为特定环境和/或自定义需求定义自定义路由。 Laminas is very handy for that too. Laminas对此也非常方便。

Have a good day.祝你有美好的一天。

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

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