How to update PHP File paths for different deployment environmentsGet the solution ↓↓↓
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.
When I call a delete_user controller in the DEV environment, currently the
<a> tag is pointing to
delete_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 the
href to be instead as
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?
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.
$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 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.
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. 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.
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.
Then in your php you have access to those variables either using
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.
People are also looking for solutions of the problem: script cache:clear returned with error code 255
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.
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.