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.
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 thehref
in<a>
tag is pointing todelete_user.php
passing the User-ID as a GET variable. Then there is a controller which runs the DELETE SQL Query, and redirects back to the calling page.
<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 thehref
to be instead asdelete-user/user-id
.
Now, the href file-paths should be updated and interpreted seamlessly across environments.
<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)
What is the best and scalable way to achieve this in PHP?
Answer
Solution:
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.
Properties :
$configKey
: universal key as text or integer, as you prefer$testValue
: value for the test step env$stagingValue
: value for the staging step env$prodValue
: value for the prod final step env
And any other steps you need.
Of course you can add setters and getters and a custom constructor to build an object.
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 usingZend Framework
, orLaminas
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.
There are of course many view helpers, but the url view helper can show you many advantages in a daily work basis. 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 customApplicationLinksConfiguration
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. Or one ring for hrefs, one ring to build all your your hrefs.
And the last way is to define custom routes for specific env and/or custom needs.Laminas
is very handy for that too.
Have a good day.
Answer
Solution:
You can add environment variables in your php-fmp settings and read those in your code. 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
Then in your php you have access to those variables either using$_ENV
orgetenv()
https://www.php.net/manual/en/reserved.variables.environment.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 tovar_dump($_SERVER)
in your dev environment and in your prod environment and find a field that can work for you.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php_network_getaddresses: getaddrinfo failed: temporary failure in name resolution
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.