How to define Global or constant Variables in Laravel 5?

How to define Global or constant Variables in Laravel 5?

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

In this tutorial, i will let you know how to define global variable in Laravel 5.

As you know, Laravel is going very popular framework because of their good features and have lots of method that save your time.

Many time you need to define global variable which you can use everywhere in your project.

For Example, If you are working in admin area and everywhere you use pagination to list some record at a time and you have to pass everywhere record per page in paginate() method to render some record at a time so instead of passing manually you can define them in single file and use them anywhere where you need it.

There are so many situation like you need to use some api credentials and you define everywhere your APP_KEY and your APP_SECRET so here in this situation you can manage all these records from single file and whenever you want to change details then you can change from one place where you defined it. In Laravel you can easily do this.


Create a file constants.php in config directory :



config/constants.php



return [
	
	'APP_NAME' => 'My APP',
	'APP_KEY' =>'xxxxx-xxxx',
	'APP_SECRET' => 'xxxxxxx-xxxxxx-xxxx'
];



As i told you that Laravel has lots of helper function that save your time and config() function is one of them to gets the value of a configuration file.

You will get configuration value using ‘dot’ syntax.



Example:

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

Similarly you can define record per page value for pagination or anything you want to define as global variable. This will be accessible from everywhere.

Label :

PHP

Laravel PHP Framework

How To

MVC

Web Development

Hope this code and post will helped you for implement How to define Global or constant Variables in Laravel 5?. 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 *

  +  54  =  59

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