Skip to content

Service Provider Package Config, Views, Lang Issue #265

Closed
@bencorlett

Description

@bencorlett

Hi,

Consider the following service provider:

<?php namespace Foo\Bar;

class FooBarServiceProvider extends ServiceProvider {

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->package('foo/bar', 'foo/bar');

        $this->app['qux'] = $this->app->share(function($app)
        {
            $dependency = $app['config']['foo/bar::baz.bat'];
            new Corge($dependnecy);
        });
    }

}

This works fine. However, as @taylorotwell has mentioned, all package() commands belong in boot. Let's try this:

<?php namespace Foo\Bar;

class FooBarServiceProvider extends ServiceProvider {

    /**
     * Boot the service provider.
     *
     * @return void
     */
    public function boot()
    {
        $this->package('foo/bar', 'foo/bar');
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app['qux'] = $this->app->share(function($app)
        {
            $dependency = $app['config']['foo/bar::baz.bat'];
            new Corge($dependnecy);
        });
    }

}

This works fine. Sort of. If you try accessing $app['qux'] anywhere before a request has been dispatched (i.e. in app/routes.php), the config hasn't been loaded because boot() isn't called until a request is dispatched.

Bit of a chicken egg situation. I have run into this issue on a real life scenario where I am trying to override Laravel's router for a specific reason. When you call Route::get() (for example) in app/routes.php you run into the issue where the config hasn't been loaded from boot() yet.

Problem with not putting package() calls in boot() is that you can run into issues when service providers depend on other components from other service providers.

If anybody has a suggestion on how to get around this please let me know, but I feel that there is an underlying problem potentially.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions