Reputation: 31225
It is possible to reference another Config variable within a configuration files?
Something like this config/app.php
'user' => Config::get('mail.user'),
Upvotes: 17
Views: 7604
Reputation: 1368
No, as far as I know this isn't possible in the way you suggested. As mentioned in the other answers, you should do that by using your environment (like .env
file).
I suggest you do this in a service provider (like AppServiceProvider.php
). To me it sounds like you're doing something what isn't actually not a configuration thing. I think you better can do it this way:
As you can see in the documentation of the configuration repository there is a "set" method on the config repository. So do it like this in a service provider:
public function boot()
{
Config::set('app.user',Config::get('mail.user'));
}
Put this in the boot method so every binding is present in the IoC container.
Upvotes: 3
Reputation: 3855
While it's probably not considered best practice, I see no reason not to reference another config if it's a dependency.
One solution I've seen used in projects is to simply include()
another config file directly. Example:
<?php
// in config/app.php - we've configured the app name:
return [
'name' => env('APP_NAME', 'Awesome Site'),
];
<?php
// in our custom config/html.php - we'll use the configured
// app name above to build our website's <title> value
// first include the app config
$appConfig = include(config_path('app.php'));
return [
// now we can utilize $appConfig like an array, because it is one!
// Note: whatever is return'd from a file is what include() returns
// ...the PHP manual for the include() function explains this behavior
// search php.net/manual/en/function.include.php for "Handling Returns"
'title' => $appConfig['name'] . ' - The Best Website in Existence',
// or I prefer data_get() which is safer than direct array key access
// and sort of acts like config() in that it lets us define a default:
'title' => data_get(
$appConfig,
'name',
'A Pretty OK Site - One of the Most Decent Websites on the Planet'
),
];
Now we can call config('html.title')
anywhere in our app and we'll have our site title that utilized the configured app name.
header.blade.php:
<html>
<head>
<title>{{ config('html.title') }}</title>
...
A small caveat: we need to watch out for circular dependencies. If we include('config/html.php')
in config/app.php
we'll cause an infinite loop!
A cool thing about this is you're not double loading app.php
into the app config or anything, you're just using the file in another place. So there's no need to worry about Laravel repeatedly loading app.php
or anything.
This maybe isn't the "cleanest" or most "proper" solution, but it's super simple, and I think it's a reasonable approach for the times you need it.
Upvotes: 3
Reputation: 827
In Laravel 4 possible to set variables from other config file, but you need to use:
'url' => \Illuminate\Support\Facades\Config::get('constants.url'),
instead of:
'url' => Config::get('constants.url'),
Upvotes: 0
Reputation: 2653
Try adding a mail.php
file in your app/config directory:
<?php
return array(
'user' => 'username'
);
And then get the value like you proposed:
'user' => Config::get('mail.user'),
Upvotes: -2