Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining constants in laravel

In laravel, there is no constant file defined, so I went on and searched for a way to implement the use of constants. The method below is what I managed to put together:

// app/config/constants.php
return['CONSTANT_NAME' => 'value'];

// index.blade.php
{{ Config::get('constants.CONSTANT_NAME') }}

Now, my question is; is there a cleaner way of retrieving my constants in my view? Something like:

{{ Constant::get('CONSTANT_NAME') }}

This is in order to keep my view nice, short and clean.

Appreciate the input!

like image 726
Marfat Avatar asked Sep 26 '14 15:09

Marfat


2 Answers

One thing you can do is to share pieces of data across your views:

View::share('my_constant', Config::get('constants.CONSTANT_NAME'));

Put that at the top of your routes.php and the constant will then be accessible in all your blade views as:

{{ $my_constant }}
like image 89
msturdy Avatar answered Sep 30 '22 14:09

msturdy


In v5 you can do like @msturdy suggests except you store the constant in the .env file or in production as actual $_ENVIRONMENT variables on your server for your environment.

Example .env entry:

CONSTANT=value

Then call like so:

View::share('bladeConstant', env('CONSTANT'));

Then load it with:

{{ bladeConstant }}
like image 23
Joshua Fricke Avatar answered Sep 30 '22 14:09

Joshua Fricke