Laravel Configuration

Written by Laravel Master on Jul 12th, 2020 Views Report Post

Laravel offers many configuration options. When you create a new laravel application, you will have a folder located in the root of the app named config, inside of this folder you will see a ton of default configuration files, all of which are configuration settings for your application.

An easy file to remember is the config/app.php. If you are just getting started with Laravel, this one will probably be simpler to understand. This file will contain things like your application name or your application language.

Getting a config value

You can use the config helper function to retrieve information from any configuration file. For instance, to get the name of your app, you can do the following:

config('app.name');

If you were to echo out the contents of the code above, you would get the contents of that string name located in an array inside the config/app.php file.

There is another way that you can retrieve a configuration value, and that's by using the \Config class, like so:

\Config::get('app.name')

The above code will give you the same output as using the config() helper function.

Setting a config value

If you want to change a configuration manually, you can manually modify the string in the config file.

An alternative to hardcoding the value, is to change a configuration dynamically, and in that case, you can do so with the config helper function:

config(['app.name' => 'My Radical Application']);

Additionally, you may also do this with the \Config class, like so:

\Config::set('app.name', 'My Radical Application');

Quick note: dynamically changing your configuration file may result in some strange behavior if it's not managed well, which is why a lot of developers will tell you not to edit tour config dynamically. But hey, if it fits your use case, do it.

Your Environment File

There is one more important file that you must be aware of when adding configurations to your Laravel application, and that is the .env file. With a new install of a laravel application, you may see a file named: .env.example, in most cases you will need to rename that to be .env, and this is the file that contains all your Environment configuration settings.

What is the Environment Configuration?

An environment is a specific location where your application is running. So, if you are working on your local machine, your environment will be local, if it is a live application on a server, then your environment will be production. Each environment may need to have different settings (you may need to connect to another database on your local machine, than your production machine)

So, this is where you'll store all of your application environment configurations.

Here is an example of a default .env file in a Laravel application:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Now, many of your application config files inside of the config folder will pull data from this .env file. Take a look at the beginning of the config/app.php file, which will look like this:

<?php

return [

    /*
    |------------------------------------------------------
    | Application Name
    |------------------------------------------------------
    |
    | This value is the name of your application. This value 
    | is used when the framework needs to place the 
    | application's name in a notification or any 
    | other location as required by the 
    | application or its packages.
    |
    */

    'name' => env('APP_NAME', 'Laravel'),

    /*
    |------------------------------------------------------
    | Application Environment
    |------------------------------------------------------
    |
    | This value determines the "environment" your 
    | application is currently running in. This 
    | may determine how you prefer to configure 
    | various services the application utilizes. 
    | Set this in your ".env" file.
    |
    */

    'env' => env('APP_ENV', 'production'),

    /*
    |------------------------------------------------------
    | Application Debug Mode
    |------------------------------------------------------
    |
    | When your application is in debug mode, detailed error 
    | messages with stack traces will be shown on every 
    | error that occurs within your application. If 
    | disabled, a simple generic error page 
    | is shown.
    |
    */

    'debug' => (bool) env('APP_DEBUG', false),

You can see that the first value returned name, looks like this:

'name' => env('APP_NAME', 'Laravel'),

The value of name will check our .env file for a key named APP_NAME, if it exists that will be the value of config('app.name'), otherwise it will default to the second argument.

Conclusion

That's the basics of your configuration files in a Laravel application. You may also want to check the official Laravel Documentation on Configurations to learn more.

And as always, you can dig deeper into the actual logic of the application to learn more about how everything works.

Comments (0)