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 :
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') }}