Laravel - 存储库的绑定文件

时间:2015-12-16 17:58:31

标签: php laravel binding

所以我有一个App \ bindings.php文件,我已经添加到我的composer.json文件中了:

"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'));

但是,我运行composer dump-auto并尝试运行我的应用程序,我收到以下错误:

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

好像它正在使用全局配置助手方法......但我不知道如何解决这个问题。更好的是,如果你知道我怎么能以另一种方式做到这一点,我全都听见了。

1 个答案:

答案 0 :(得分:1)

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

在AppServiceProvider中使用它