02 Apr

Defining Global Variables in Laravel 5

In this tutorial, i am going to show you different methods of defining global variables in Laravel 5. Here, i will basically discuss two methods, one using the .env file and other defining variables in routes.php file.

1) Using .env file

If you want to define app level configuration variables in Laravel 5 then you can use .env file located in project’s root directory. When you install Laravel 5 then you will get a .env.example file in project’s root directory. In order to use it, you need to rename it to .env

By default the .env file looks something like this:

These are the list of variables which are used by application and are accessible throughout the application. In this file you can define your own variables, like in above example i have defined MY_GLOBAL_VAR and one can access its value anywhere in application like this:

2) Using routes.php file

There are scenarios when you want your application level variables to be dynamic in nature(which is not possible in .env file). Suppose you want to make public path to a specific folder (‘thumbs‘) available throughout the application then you can do something like this:

Just define them in app/Http/routes.php file and you can access them anywhere in application like this:

Hope this was helpful :)
If you know any other way of defining global variables in Laravel 5 then do share with us in comments.

 

 

The following two tabs change content below.

Vineet Garg

Sr. Web Developer at GuestHouser
I am passionate about technology and web development.

Latest posts by Vineet Garg (see all)

  • Elliot Sneeringer

    Vineet,

    Thanks for your tutorial, this is exactly what I was looking for. But, I do have a question about the .env method. I am using the .env method because my application will be living in 4 different environments (local, dev, staging, prod). I will need different DB credentials and also different URL endpoints that interact with different systems to run based on which environment it’s running in. Would I use the same file and write multiple blocks of Global vars? Like so:

    APP_ENV=local
    APP_DEBUG=true
    APP_KEY=SomeRandomString

    DB_HOST=localhost
    DB_DATABASE=homestead
    DB_USERNAME=homestead
    DB_PASSWORD=secret

    CACHE_DRIVER=file
    SESSION_DRIVER=file
    QUEUE_DRIVER=sync

    MAIL_DRIVER=smtp
    MAIL_HOST=mailtrap.io
    MAIL_PORT=2525
    MAIL_USERNAME=null
    MAIL_PASSWORD=null

    followed by another set for staging?

    APP_ENV=staging
    APP_DEBUG=true
    APP_KEY=SomeRandomString

    DB_HOST=whatever staging is
    DB_DATABASE=homestead
    DB_USERNAME=homestead
    DB_PASSWORD=secret

    CACHE_DRIVER=file
    SESSION_DRIVER=file
    QUEUE_DRIVER=sync

    MAIL_DRIVER=smtp
    MAIL_HOST=mailtrap.io
    MAIL_PORT=2525
    MAIL_USERNAME=null
    MAIL_PASSWORD=null

    • http://www.techigniter.in Vineet Garg

      Yes Elliot, .env files should be ignored by version control. That way you can keep separate .env files for your local and server environment.

      • Elliot Sneeringer

        Thanks for the reply. In your experience do you set global variables using a file in /config and setting the values to the env values?

        • http://www.techigniter.in Vineet Garg

          Yes, using a global variable file in /config is the ideal way of doing it. But, as i mentioned in the post, when i need my global variables to be dynamic in nature then i set them in routes.php file.

  • Pingback: Define Global Variables in Laravel 5()

  • kapil verma

    Hi , i wanted to use a global variable in migrations file where i can define string field length gloablly. Like all name fields should be 50 character. So i can define a variable with 50 and use it with migration file

    • http://www.techigniter.in Vineet Garg

      You can define constants in you app/routes.php file like
      define(‘FIELD_LENGTH’, 50);
      and in migration just use the defined constant.

  • Gazzer

    These examples are for global constants rather than global variables.