geoffs3310 geoffs3310 - 6 months ago 62
PHP Question

Adding a new config file in Laravel 5 not working

I want a new config file in my Laravel 5 app to store all my constants in. After looking around the net I found the recommended solution seems to be to create a new config file that returns an array of key value pairs and then use that. So I created the following file:

<?php
// config/constants.php

return [
'SITE_NAME' => 'Site Name',
'SITE_EMAIL' => 'email@site.com',
'ADMIN_EMAIL' => 'admin@site.com'
];


Then in one of my controllers I try to access one of these values like so:

echo Config::get('constants.ADMIN_EMAIL');


I just get the following error:

FatalErrorException in WelcomeController.php line 46:
Class 'App\Http\Controllers\Config' not found


Do I have to do something else to get it to work?

Answer

In Laravel 5, to avoid this kind of headache, you can use the config helper function to get a config item, like this :

config('constants.ADMIN_EMAIL')

Nice and easy ;)