laravellaravel-5

Laravel config file of a module


I need to get the keys of a file inside the config of a module. I created User module using nWidart package. This is the registerConfig function

protected function registerConfig()
{
    $this->publishes([
        __DIR__.'/../Config/config.php' => config_path('user.php'),
    ], 'config');
    $this->mergeConfigFrom(
        __DIR__.'/../Config/config.php', 'user'
    );
}

I created a file named menu.php that returns an array with a key items, then tried this

dd(config('user::config.name'))
dd(config('user::menu.items'))

They both return null. I also tried php artisan vendor-publish of UserServiceProvider but it still returns null. How can I return the keys?

update

If I simply do: config() I get a full array of the config arrays and can see my package config file there. But the menu config file is not in there


Solution

  • In Your serviceProvider, register method.

        $this->mergeConfigFrom(
            __DIR__ . '/../config/master.php',
            'master'
        );
    
        // Merge configs
        $this->mergeConfigFrom(
            __DIR__ . '/../config/slave.php',
            'slave'
        );
    

    Create two files inside config folder slave.php

        <?php
    return [
        'menu' => [
            'OP1',
            'OP2',
            'OP3',
            'OP4',
        ]
    ];
    

    master.php //any other config you want to load

    Run php artisan config:cache

    and use

    dd(config('slave.menu')); dd(config('master.XXX'));

    This will output

    array:4 [▼
      0 => "OP1"
      1 => "OP2"
      2 => "OP3"
      3 => "OP4"
    ]