How to define Global Variables in Laravel?

How to define Global Variables in Laravel?

In this post we will give you information about How to define Global Variables in Laravel?. Hear we will give you detail about How to define Global Variables in Laravel?And how to use it also give you demo for it if it is necessary.

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.


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.


Also see:How to set global view variables using composer share in Laravel?

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

Hope this code and post will helped you for implement How to define Global Variables in Laravel?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

9  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US