Reputation: 418
I started to create Admin login laravel package for learning purpose, In this package i need to add add guards and providers to the auth.php file from my package.
From Laravel documentation, I got to Know we can add configuration file to project config folder from service provider boot method as follow.
public function boot()
{
$this->publishes([
__DIR__.'/path/to/config/courier.php' => config_path('courier.php'),
]);
}
But how to add my config file to merge with config/auth.php file.
And what the following lines of code will do, i am not getting from laravel documentation.
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/path/to/config/courier.php', 'courier'
);
}
And following is my custom service provider code
<?php
namespace Abhilash\AdminLogin;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Arr;
class AdminLoginServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__.'/config/admin.php' => config_path('auth.php'),
]);
}
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/config/admin.php', 'auth'
);
}
/**
* Merge the given configuration with the existing configuration.
*
* @param string $path
* @param string $key
* @return void
*/
protected function mergeConfigFrom($path, $key)
{
$config = $this->app['config']->get($key, []);
$this->app['config']->set($key, $this->mergeConfigs(require $path, $config));
}
/**
* Merges the configs together and takes multi-dimensional arrays into account.
*
* @param array $original
* @param array $merging
* @return array
*/
protected function mergeConfigs(array $original, array $merging)
{
$array = array_merge($original, $merging);
foreach ($original as $key => $value) {
if (! is_array($value)) {
continue;
}
if (! Arr::exists($merging, $key)) {
continue;
}
if (is_numeric($key)) {
continue;
}
$array[$key] = $this->mergeConfigs($value, $merging[$key]);
}
return $array;
}
}
Upvotes: 1
Views: 1049
Reputation: 2129
I wouldn't merge configs as it doesn't merge multidimensional arrays and depending on the content of these configs or their complexity, it may get messy. Summary, It will merge the content of $key
with $value
of your config. But what you can do is either override
the mergeConfigFrom
in your ServiceProvider
or you can create a new function and use it on the register
, like such:
/**
* Merges the configs together and takes multi-dimensional arrays into account.
*
* @param array $original
* @param array $merging
* @return array
*/
protected function mergeConfigs(array $original, array $merging) {
$array = array_merge($original, $merging);
foreach ($original as $key => $value) {
if (! is_array($value)) {
continue;
}
if (! Arr::exists($merging, $key)) {
continue;
}
if (is_numeric($key)) {
continue;
}
$array[$key] = $this->mergeConfig($value, $merging[$key]);
}
return $array;
}
Disclaimer: I'm not the author but I've used this function once and it works as expected, here's the original
Upvotes: 1