Better PHP Development by Bruno Skvorc

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



Copyright Disclaimer:
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.