Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Global variables in Laravel 5

Tags:

php

laravel

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.

like image 281
Santosh Achari Avatar asked Nov 29 '22 06:11

Santosh Achari


1 Answers

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') }}
like image 57
Santosh Achari Avatar answered Dec 06 '22 08:12

Santosh Achari