In the laravel tutorial for beginners part 5, we have learned about environment variables, in this tutorialĀ we will learn about the application environment and configuration. When we create an application, sometimes we want to determine which environment it is currently running in, for example locally, dev, staging,QA or Production. In laravel this is usually done using the APP_ENV environment variable, to check what environment it is currently running in, we can use the function App::environment(value) or App::environment([value1, value2]), which will return true value, this is useful when we need a logic, when we are in local, what are we doing or when we are in production, what are we going to do, and so on.
PHP Laravel Tutorial(Application environment and Configuration)
Table of contents :
- Create the unit tests for the Application Environment
- Configuration
Create unit tests for the application environment
The next stage in this PHP Laravel tutorial is to create a unit test for the application environment. If we take notice when we use the unit test, in phpunit.xml actually the application environment has been hardcoded and given the value “testing” (<env name=āAPP_ENVā value=ātestingā/>) as shown below

To test it, we will make an integration test with the command: “php artisan make: test app_environment_test” then edit and paste the code below
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Support\Facades\App;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class app_environment_test extends TestCase
{
/** @test */
public function test_app_env()
{
var_dump(App::environment()); //show the APP_ENV value
}
}
oh yeah, don’t forget to import the class with the code “use Illuminate\Support\Facades\App;” If we run the unit test above, it will appear as shown below

From the results of the unit test above we are in the “Testing” Environment, if we look at the .env file, the APP_ENV value is “Local” as shown below

But why from the unit test results above the APP_ENV value is “testing” even though in the .env file the APP_ENV value is “local” ???
the answer is because the value of APP_ENV was overwritten by phpunit.xml while running the unit test.
Configuration folder
Yhe Laravel stores all configurations in the config folder contained in the project and the prefix of the configuration starts with the php file contained in the project. like the picture below

Create Configuration Files
To create a configuration file, we simply create a php file in the config folder and then within that file we simply return the configuration in the form of an array.
Create a php file with the name contoh.php in the config folder in your project then write the code below
<?php
return[
"name" => [
"first"=>"Gede"
"last"=>"Suryawan"
],
"email"=>"gedebhama@gmail.com",
"web"=>"https://webbaliseo.com"
];
?>
To retrieve the configuration in the config file, we will use a function from Laravel, namely the config(key, default) function, which creates a key in the config starting with the file name, then
followed by the key contained in the return value and each nested array using a period (.)
for example in the example configuration file “contoh.php” above, if we want to take the value “first” then write the code to be “contoh.name.first” which means we take the configuration from the file contoh.php, then we take the data array key “name” then we take inside it data key “first”.
Just like the env() function, the config() function also has a default parameter value if the configuration key value is not available, now we will try to create a unit test to retrieve the configuration values, open the terminal and create a unit test with the code āphp artisan make: test configurationtestā after that open the configurationtest.php file in the test/feature/configurationtest.php folder then type the code below
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class configurationtest extends TestCase
{
/** @test */
public function configtest()
{
$firstname = config('contoh.name.first'); //get value of key 'first' in file config/contoh.php
self::assertEquals('Gede', $firstname); //check the value of $firstname (key first)
}
}
If the unit test above is run, it will look like the image below

This the PHP Laravel tutorial (Application environment and Configuration) I hope it’s useful and see you in the Laravel learning tutorial part 7