[Solved] How to get config data in laravel in a subfolder

I have the Laravel Administrator Plugin and I set up a setting administrator page that is located in:

app/config/administrator/settings/site.php

The application can store the data, but when i try to get some data in one of my controllers like this:

Config::get(‘administrator.settings.site’)

I can get a returned value… Always null or array 0.

How I can get those configuration values?

Solution #1:

Solution:

You can use a file path rather than dot notation for file paths:

Config::get('administrator/settings/site.variable_name');

Some Explanation

Dot notation is used to get variables inside the config array within a file, rather than to denote file paths.

Therefore you can use Config::get('administrator/settings/site.variable_name'); to get $variable_name from administrator/settings/site.php, or ENV_DIR_NAME/administrator/settings/site.php depending on your environment.

Directories within in app/config are read as environments. For example app/development/database.php will work with Config::get('database.whatever') when you’re in your “development” environment while app/production/database.php will be read with Config::get('database.whatever') when you’re in the “production” environment.

Laravel falls back to the config/config/*.php file when no environment-specific file is present.

Note

I believe that admin package has a config file in app/config/packages/frozennode/administrator/administrator.php which the package would like you to use for it’s settings. That should be available using this convention: Config::get('package::file.option');

Respondent: fideloper
Solution #2:

I ran into a very similar problem when using this Laravel-Excel package.

The solution I needed was slightly different to the solution above.

I needed to take advantage of Laravel’s config overriding feature to have one config setting for normal execution and a different one for testing. Specifically, I normally wanted Excel files to be stored in storage/excel and I wanted them to be put in storage/testing/excel under testing.

The technique of using a slashed path didn’t work because it points to a specific file so didn’t respect the different environments:

$directory = Config::get('packages/maatwebsite/excel/export.store.path');

The solution was to use a package prefix so that the config loader would respect the environment:

$directory = Config::get('excel::export.store.path');

I’m not exactly sure where the excel shorthand name comes from but I suspect it’s something to do with the Excel facade in the package exposing itself as 'excel'.

Respondent: Matthew
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.