Better PHP Development by Bruno Skvorc
Author:Bruno Skvorc
Language: eng
Format: epub
Publisher: SitePoint
Published: 0101-01-01T00:00:00+00:00
But How About the Configuration?
As explained earlier, we can pass in parameters as an associative array to the AnnotationBeanFactory class.
To manage configuration in our framework, we create two configuration files, one for development and one for the production environment.
Each file returns an associative array, which we can be loaded into a variable.
Let's keep them inside Config directory:
// Config/dev.php return [ 'debug' => true; ];
And for production:
// Config/prod.php return [ 'debug' => false; ];
To detect the environment, we'll specify the environment in a special plain-text file, just like we define an environment variable:
ENV=dev
To parse the file, we use PHP dotenv, a package which loads environment variables from a file (by default the filename is .env) into PHP's $_ENV super global. This means we can get the values by using PHP's getenv() function.
To install the package:
composer require vlucas/phpdotenv
Next, we create our .env file inside the Config/ directory.
Config/.env
ENV=dev
In the front controller, we load the environment variables using PHP dotenv:
<?php //web/index.php // ... // Loading environment variables stored .env into $_ENV $dotenv = new Dotenv\Dotenv(__DIR__ . '/../Config'); $dotenv->load(); // Load the proper configuration file based on the environment $parameters = require __DIR__ . '/../config/' . getenv('ENV') . '.php'; $container = new \bitExpert\Disco\AnnotationBeanFactory(Services::class, $parameters); \bitExpert\Disco\BeanFactoryRegistry::register($container); // ...
In the preceding code, we first specify the directory in which our .env file resides, then we call load() to load the environment variables into $_ENV. Finally, we use getenv() to get the proper configuration filename.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6833)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6561)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6430)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(3998)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3789)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3734)
The Ultimate iOS Interview Playbook by Avi Tsadok(3708)
