简体   繁体   English

Laravel 4个单独的软件包配置文件

[英]Laravel 4 separate config files for packages

My laravel 4 project has multiple environments set up. 我的laravel 4项目设置了多个环境。

The environments setup are: development is called 'development' and production is called 'production'. 环境设置为:开发称为“开发”,生产称为“生产”。 In my app/config folder, I have two extra folders. 在我的app / config文件夹中,我有两个额外的文件夹。 One for each name of the environment. 每个环境名称一个。

See screenshot: 看截图:

在此处输入图片说明

Inside these folders I have separate config files for database.php and app.php. 在这些文件夹中,我有分别用于database.php和app.php的配置文件。 These are working as expected. 这些正在按预期方式工作。 Laravel uses my local database configuration when running locally and production when running live. Laravel在本地运行时使用我的本地数据库配置,在实时运行时使用生产。

Now, I've added a new package which requires a config file. 现在,我添加了一个需要配置文件的新软件包。 This resides in /app/config/packages/greggilbert/config.php . 它位于/app/config/packages/greggilbert/config.php

I need to be able to set different config files the same way I do for the database. 我需要能够以与数据库相同的方式设置不同的配置文件。 So, I assumed creating the same file structure in /development or /production would work, but this does not seem to be the case. 因此,我假设在/development/production创建相同的文件结构是可行的,但事实并非如此。

Here is a new screenshot: 这是一个新的屏幕截图:

在此处输入图片说明

Package-related, environment-specific configuration files have to be placed in the package's directory within a subdirectory named after the environment. 与程序包相关的,特定于环境的配置文件必须放置在程序包目录中以环境命名的子目录中。 So in your case this would be 所以在你的情况下

app
   - config
      - packages
         - greggilbert
            - recaptcha
               - development
                  - config.php
               - production
                  - config.php

But it can get really ugly and cumbersome in some cases. 但是在某些情况下,它可能会变得非常丑陋和繁琐。 This can be solved in a (in my opinion) cleaner and more elegant way through enviroment files in your application root directory: 这可以通过(在我看来)一种更干净,更优雅的方式通过应用程序根目录中的环境文件来解决:

/.env.development.php /.env.development.php

<?php
return [

    // any configuration settings for your local environment

    'RECAPTCHA_TEMPLATE' => 'customCaptcha',
    'RECAPTCHA_LANGUAGE' => 'en',

];

That way, you could leave the package directory as it is: 这样,您可以将包目录保留为:

app
   - config
      - packages
         - greggilbert
            - recaptcha
               - config.php

Laravel will load the environment file according to the environment and make the environment variables globally available through getenv() . Laravel将根据环境加载环境文件,并通过getenv()使环境变量全局可用。 So now you could just grab the specified template for the current environment with getenv('RECAPTCHA_TEMPLATE') from the config.php. 因此,现在您可以使用config.php中的getenv('RECAPTCHA_TEMPLATE')获取当前环境的指定模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM