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条)

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本