Skip to content

Instantly share code, notes, and snippets.

@ceilidhboy
Created November 10, 2023 10:58
Show Gist options
  • Save ceilidhboy/95f30d535377bad2155e11f705e9e5ee to your computer and use it in GitHub Desktop.
Save ceilidhboy/95f30d535377bad2155e11f705e9e5ee to your computer and use it in GitHub Desktop.
How to define constants safely in Laravel config files

Doing a simple CONST declaration in config files can cause errors when enabling caching in Laravel. The config file appears to be loaded more than once and the second time causes an error because the constant is already defined.

The safe way to do it is to use the old syntax and only try to define the constant if it has not already been defined, as the following example shows:

<?php

use Illuminate\Support\Facades\Facade;

if (!defined('DEFAULT_PHONE_NO'))
    define('DEFAULT_PHONE_NO', '0123456789');

if (!defined('DEFAULT_EMAIL'))
    define('DEFAULT_EMAIL', '[email protected]');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment