How to define Global Variables in Laravel 5?

By Hardik Savani | April 17, 2016 | | 3752 Viewer | Category : Laravel


Share this post:

Laravel framework is awesome framework in todays. Laravel provide sevaral good feature and that's why it is very popular framework in PHP. Basically if we are work on big website project or ERP level project we always need to define some global variable like site title, number of pagination, footer text etc as depend on our project. So, I am going to show you how to define and use global variable in Laravel 5. Laravel 5 provide very simple way to set Global Variables in our laravel application. It is very pretty simple and with good directory structure.

First we have to create global.php file in config folder of your laravel 5 application. I did add three variable in that file : siteTitle, pagination and tagLine. you can also add more then one on that file. In this example you can access that variable in whole application, i mean you can get global variable value on controller, view, model and anywhere. so copy bellow code and put in global.php file.

config/global.php

return [

'siteTitle' => 'HD Site',

'pagination' => 5,

'tagLine' => 'Do the best'

];

After creating this file you can use using config() helper. in bellow line you can put anywhere and get "siteTitle" value, same way you can get other value too.

Example:

{{ config('global.siteTitle') }}



We are Recommending you: