Santosh Achari
Santosh Achari

Reputation: 3006

Set Global variables in Laravel 5

What is the best way to add variables to Laravel framework that is accessible across Controllers and Views?

I don't want to use .env for storing the variables as it wouldn't be available via Git.

Upvotes: 4

Views: 16736

Answers (1)

Santosh Achari
Santosh Achari

Reputation: 3006

You can create a file within config folder. e.g.

config
|- constants.php

Inside constants.php you can define your global variables

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | User Defined Variables
    |--------------------------------------------------------------------------
    |
    | This is a set of variables that are made specific to this application
    | that are better placed here rather than in .env file.
    | Use config('your_key') to get the values.
    |
    */

    'company_name' => env('COMPANY_NAME','Acme Inc'),
    'company_email' => env('COMPANY_email','[email protected]'),


];

You can access these variable using either of these two functions:

Config::get('constants.company_name')
config('constants.company_name')

For Blade as well:

{{ config('constants.company_email') }}

Upvotes: 24

Related Questions