duanlong4890 2015-11-13 18:10
浏览 53
已采纳

Laravel 5访问mail.php中的全局变量

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

<?php
return array(
    'admin_email' =>'joe@doe.com',
    '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.

  • 写回答

2条回答 默认 最新

  • douduiyun6125 2015-11-13 19:35
    关注

    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:

    ADMIN_EMAIL=joe@doe.com
    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?