【问题标题】:How to change Laravel Container properties from within Service Provider?如何从服务提供者中更改 Laravel 容器属性?
【发布时间】:2019-05-11 01:23:50
【问题描述】:

Laravel 像这样将 pathis 绑定到容器中

$this->instance('path.lang', $this->langPath());   

在 /vendor/laravel/framework/src/Illuminate/Foundation/Application.php 中的 bindPathsInContainer() 中,我想将此路径从我的服务提供者中更改为其他路径。

我知道容器实例可以通过 $this->app 从服务提供者中访问,所以我想我可以这样做

$this->app->instance('path.lang', 'my/path');

然而,这会返回“类实例不存在”。谁能解释这里发生了什么? 另外,为什么要引用容器的任何属性,如

$this->app->someProperty

导致这个错误?有没有办法从服务提供商内部编辑 someProperty?注意:我知道这可以通过扩展 \Illuminate\Foundation\Application 来完成,正如 here 所解释的那样,但我想这样做,而不必触及 Laravel 的默认文件。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    我认为路径必须是绝对的:

    php artisan tinker
    app()->instance('path.lang', '/home/user/you/project/something/something/that/exists);
    

    你也可以在你的 AppServiceProvider 中这样尝试:

    $this->app->instance('path.lang', $this->app->resourcePath().DIRECTORY_SEPARATOR.'mylang');
    

    【讨论】:

      猜你喜欢
      • 2017-01-11
      • 2019-05-29
      • 2018-08-28
      • 2011-08-05
      • 1970-01-01
      • 2016-12-21
      • 2015-10-24
      • 2016-10-06
      • 1970-01-01
      相关资源
      最近更新 更多