Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel package reading package config file and not published config file

I have created a Laravel package, uploaded it to packagist and managed to install it using composer require.

I have now hit a problem and I don't know how to fix it and doing a search does not help.

I have a config file which publishes a default config file to the config directory. I have made changes to the published file and now I want my package to use this config file but it's using the config file within the package and not the newly updated published file. This is my service provider within the vendor src folder

namespace Clystnet\Vtiger;

use Illuminate\Support\ServiceProvider;

class VtigerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->publishes([
            __DIR__ . '/Config/config.php' => config_path('vtiger.php'),
        ], 'vtiger');

        // use the vendor configuration file as fallback
        $this->mergeConfigFrom(
            __DIR__ . '/Config/config.php', 'vtiger'
        );
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('clystnet-vtiger', function () {
            return new Vtiger();
        });

        config([
            'config/vtiger.php',
        ]);
    }
}

This is my main package class

<?php 
namespace Clystnet\Vtiger;

use Storage;
use Illuminate\Support\Facades\Config;

class Vtiger
{
    protected $url;
    protected $username;
    protected $accesskey;

    public function __construct() {
        // set the API url and username
        $this->url = Config::get('vtiger.url');
        $this->username = Config::get('vtiger.username');
        $this->accesskey = Config::get('vtiger.accesskey');
    }
   ...

Within my class I'm doing a var_dump($this->url) and it's not reading the correct config file.

How do I set it to use the right one?

UPDATE

This is my custom config file and the one that the package is reading

return [
    'url' => 'path/to/vtiger/webservice',
    'username' => '',
    'accesskey' => '',
];
like image 700
AdRock Avatar asked Dec 23 '22 08:12

AdRock


2 Answers

I got the same issue ( contact : is my package )

VIMP: specify package_name.php as your config filename (eg: contact.php)

step 1: First, move your "mergeConfigFrom()" method to "register()" method

public function register()
{
    $this->mergeConfigFrom(
        __DIR__.'/config/contact.php','contact'
    );
}

step 2: remove your published config file from the config folder

step 3: publish it again by using vendor:publish

php artisan vendor:publish

step 4: clear your caches

php artisan cache:clear

php artisan config:clear

step 5: Now you can access your config value

$value = config('contact.default.send_email_to');

step 6: My config file is

<?php
    return [
        "default"=>[
            "send_email_to" => "[email protected]"
        ]
    ];
like image 124
Manu Joseph Avatar answered Dec 28 '22 23:12

Manu Joseph


As docs says you should put it on the register() method like:

public function register()
{

  // use the vendor configuration file as fallback
  $this->mergeConfigFrom(
      __DIR__ . '/Config/config.php', 'vtiger'
  );

  ...

}

This should fix the problem.

BTW you need to take care about multi-dimensional arrays because docs says:

This method only merges the first level of the configuration array. If your users partially define a multi-dimensional configuration array, the missing options will not be merged.

like image 22
Troyer Avatar answered Dec 29 '22 01:12

Troyer