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!
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 }}
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 }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With