>  기사  >  백엔드 개발  >  Laravel 개발 패키지에서 도우미 메서드 구성을 사용하는 경우 Vendor:publish를 실행할 때 오류가 보고됩니다.

Laravel 개발 패키지에서 도우미 메서드 구성을 사용하는 경우 Vendor:publish를 실행할 때 오류가 보고됩니다.

WBOY
WBOY원래의
2016-12-01 00:56:421309검색

공급자를 사용하여 서비스를 등록하는 패키지를 직접 개발했지만 구성을 가져오는 데 config() 메서드가 사용되었습니다. 문제는 구성 파일이 아직 게시되지 않았다는 것입니다. 내 생각이 틀렸다고 생각합니다.
코드는 다음과 같습니다.

<code>class SuperViewConfigProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Publish the config file to 
        $this->publishes([
            __DIR__.'/../../config/config.php' => config_path('superview.php'),
        ]);
    }
}</code>
<code>class SuperViewModelProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // Get config, then bind automaticly
        $models = array_keys(config('superview.models'));
        foreach ($models as $model) {
            $this->app->singleton(config('superview.model_prefix') . $model, function($app) use ($models, $model) {
                return new $models[$model];
            });
        }
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array_map(function($value) {
            return config('superview.model_prefix') . $value;
        }, array_keys(config('superview.models')));
    }
}</code>

그런 다음 다음을 실행합니다: php artisan Vendor:publish --provider="SuperViewProvidersSuperViewConfigProvider"
구성 콘텐츠가 존재하지 않는다는 오류가 보고됩니다.

답글 내용:

공급자를 사용하여 서비스를 등록하는 패키지를 직접 개발했지만 구성을 가져오는 데 config() 메서드가 사용되었습니다. 문제는 구성 파일이 아직 게시되지 않았다는 것입니다. 내 생각이 틀렸다고 생각합니다.
코드는 다음과 같습니다.

<code>class SuperViewConfigProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Publish the config file to 
        $this->publishes([
            __DIR__.'/../../config/config.php' => config_path('superview.php'),
        ]);
    }
}</code>
<code>class SuperViewModelProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // Get config, then bind automaticly
        $models = array_keys(config('superview.models'));
        foreach ($models as $model) {
            $this->app->singleton(config('superview.model_prefix') . $model, function($app) use ($models, $model) {
                return new $models[$model];
            });
        }
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array_map(function($value) {
            return config('superview.model_prefix') . $value;
        }, array_keys(config('superview.models')));
    }
}</code>

그런 다음 다음을 실행합니다: php artisan Vendor:publish --provider="SuperViewProvidersSuperViewConfigProvider"
구성 콘텐츠가 존재하지 않는다는 오류가 보고됩니다.

공급자의 register 메소드에서 mergeConfigFrom을 먼저 호출합니다.

<code class="php">$this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'superview');</code>

패키지의 구성 파일을 기본으로 사용한 다음 사용자 앱 디렉터리의 구성을 병합하고 마지막으로 슈퍼뷰의 구성을 가져와 현재 구성 저장소에 설정한다는 의미입니다.

ServiceProvider라고 써도 되지만, 이 파일이 있는지 확인해보시면 어떨까요?

<code>__DIR__.'/../../config/config.php</code>

config가 출시되기 전에 여기서 config를 사용할 수 있는지 잘 모르겠습니다

<code>public function provides()
{
        return array_map(function($value) {
            return config('superview.model_prefix') . $value;
        }, array_keys(config('superview.models')));
}</code>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.