How To Define Global Constant Variables in Laravel 10

Reading Time: 3 minutes
809 Views

The proper administration of global constant variables is critical in Laravel 10 development for ensuring consistency and boosting code readability. Whether you need to save configuration values, API keys, or any other static information that remains consistent throughout your application, establishing global constant variables can help you speed up development and improve code maintainability.

In this detailed tutorial, we will look at how to define global constant variables in Laravel 10. We will walk you through the implementation process step by step, allowing you to access and use these constants throughout your whole application.

Read More: How To Create Custom Blade Directive in Laravel 10 Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Define Global Constant

To define global constants in laravel application we have several ways. But we will see about two:

  • By .env file
  • By Custom Constant file

By .env file

Open .env file and define constant like this:

//...

OWT_APP="ONLINE WEB TUTOR APPLICATION"

//...

To access in application either in blade files or in controller we use helper function of env.

echo getenv("OWT_APP");

echo env("OWT_APP");


By Custom Constant file

Create constants.php inside /config folder.

Read More: How To Create Custom Helper Function in Laravel 10 Tutorial

Open constants.php and define these constants into it.

<?php 

return [
	
	'OWT_APP' => 'ONLINE WEB TUTOR APPLICATION',
	'OWT_KEY' =>'xxxxx-xxxx',
	'OWT_SECRET' => 'xxxxxxx-xxxxxx-xxxx'
];

Here, we have a set of values. These values are constants.

Next,

We need to access it inside application. We can access value in two ways –

  • Using in Controller
  • Using in Blade Layout Files

To access in controller files

echo config("constants.OWT_APP");

echo config("constants.OWT_KEY");

echo config("constants.OWT_SECRET");

To access inside blade files

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

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

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

constants.<Variable_Name> is syntax. constants is the name of file.

We hope this article helped you to learn How To Define Global Constant Variables in Laravel 10 in a very detailed way.

Read More: How To Create Custom Log File in Laravel 10 Tutorial

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.