0

I need to set admin email in many place. so I created constants.php in config folder.

<?php
return array(
    'admin_email' =>'[email protected]',
    'admin_name' =>'Admin',
);

I was able to access this in my routes.php

dd(Config::get('constants.admin_email'));

However, when I try to access it in mail.php by

'from' => [
    'address' => Config::get('constants.admin_email'), 
    'name' => Config::get('constants.admin_name')
],

I got Class 'Config' not found in mail.php.

Any suggestions? Thanks.

5
  • have you try \Config with back slash?? Commented Nov 13, 2015 at 18:14
  • Just tried, same error Commented Nov 13, 2015 at 18:15
  • 1
    Why are you defining configuration values in one configuration file just to retrieve them in another? Just define the values in your config/mail.php file. Commented Nov 13, 2015 at 18:18
  • I also need this in other place. Commented Nov 13, 2015 at 18:20
  • @daolincheng So use Config::get('mail.from.address') in that other place. Commented Nov 13, 2015 at 22:50

2 Answers 2

2

After some testing, I've found you can't use Config, \Config or config() in any files in your config folder. I believe they are not available to any of these files, but I'm not 100% sure why this is.

Regardless, to solve this issue and still have them available in other parts of your application, use env or environment variables. In your .env file, add the following:

[email protected]
ADMIN_NAME=Admin

Then, in your mail.php and anywhere else you want to use them, access them using:

'from' => [
    'address' => env('ADMIN_EMAIL'), 
    'name' => env('ADMIN_NAME')
],

You can actually see them already in use in your mail.php and other config files, so it makes sense to use what already works. Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

-1

Use

config('constants.admin_email');

2 Comments

config() and Config::get() are the same thing.
Using config() instead of using the facade rules out the possible namespacing error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.