Php Laravel-存储库的绑定文件

Php Laravel-存储库的绑定文件,php,laravel,binding,Php,Laravel,Binding,因此,我在composer.json文件中添加了一个App\bindings.php文件,如下所示: "autoload": { "files": [ "app/bindings.php" ] }, 在此文件中,我尝试为我的存储库设置绑定,如下所示: <?php function getRepoBinding($id) { $repo = "{$id}Repository"; $clientName = strtoupper(conf

因此,我在composer.json文件中添加了一个App\bindings.php文件,如下所示:

"autoload": {
    "files": [
        "app/bindings.php"
    ]
},
在此文件中,我尝试为我的存储库设置绑定,如下所示:

<?php 

function getRepoBinding($id)
{
    $repo = "{$id}Repository";

    $clientName = strtoupper(config('client.name'));
    $implementation = config('app.repo_implementation', 'Eloquent');
    $clientOverride = "App\Overrides\\{$clientName}\Repositories\\{$implementation}\\{$repo}";
    $repo = class_exists($clientOverride) ? $clientOverride : "App\Repositories\\{$implementation}\\{$repo}";
    return $repo;
}
// Repository Interface Bindings
dd(getRepoBinding('Contribution'));
App::bind('ContributionIneterface', getRepoBinding('Contribution'));

它似乎与全局配置助手方法有关…但我不知道如何修复它。更好的是,如果你知道我如何用另一种方式来做这件事,我会全神贯注。

这是因为你在应用程序启动之前加载了该文件

在AppServiceProvider中使用它

Fatal error: Uncaught Error: Call to a member function make() on null in /home/vagrant/Code/famsapi/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:54 
Stack trace: #0 /home/vagrant/Code/famsapi/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(158): app('config') 
#1 /home/vagrant/Code/famsapi/app/bindings.php(9): config('client.name') 
#2 /home/vagrant/Code/famsapi/app/bindings.php(16): App\getRepoBinding('Contribution') 
#3 /home/vagrant/Code/famsapi/vendor/composer/autoload_real.php(55): require('/home/vagrant/C...') 
#4 /home/vagrant/Code/famsapi/vendor/composer/autoload_real.php(45): composerRequire03fe235c8b3156f0c5fcebbc0d696734('90f93262f3a0ac8...', '/home/vagrant/C...') 
#5 /home/vagrant/Code/famsapi/vendor/autoload.php(7): ComposerAutoloaderInit03fe235c8b3156f0c5fcebbc0d696734::getLoader() 
#6 /home/vagrant/Code/famsapi/bootstrap/autoload.php(17): require('/home/vagrant/C...') 
#7 /home/vagrant/Code/famsapi/public/index.php(22): require('/home/vagrant/C...') 
#8 {main} thrown in /home/vagrant/Code/famsapi/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 54