2

I am taking my first steps in Laravel 5, specifically with package development. My purpose with this package is to be booted in every single request, always.

So, I started creating the service HelloWorldServiceProvider:

<?php
namespace FacebookKiller\Modules\HelloWorld;

use Illuminate\Support\ServiceProvider;

class HelloWorldServiceProvider extends  ServiceProvider{

    protected $defer = false;

    public function boot(){
        dd('hello world at boot()')
    }

    public function register()
    {
        dd('hello world at register()')
    }
}

Actually, this file is this path from root:

/Larave/app/Modules/HelloWorld/HelloWorldServiceProvider.php

Then, I edit the config/app.php file in order to add the service provider

'Illuminate\Translation\TranslationServiceProvider',
'Illuminate\Validation\ValidationServiceProvider',
// my module
'FacebookKiller\Modules\HelloWorld\HelloWorldServiceProvider'

Finally, in order to test if dd() is called, I have edited the routes.php file like:

Route::get('test', function(){
    return "Making a test request";
});

The result is negative. The Service provider is not loaded. According to docs:

Deferring the loading of such a provider will improve the performance of your application, since it is not loaded from the filesystem on every request. To defer the loading of a provider, set the defer property to true

So, I make the opposite setting protected $defer to false. But the dd() function is not triggered. It is important to use Service Provider type because by that way I could load lang, views and config files from it.

2
  • 1
    open storage/framework/services.json file, did you see your provider class in this file ? If not, delete this file, laravel will generate it again. Commented Feb 6, 2015 at 7:50
  • @MartirosAghajanyan, you fix it! Thank you! Please, create the answer to accept it. Commented Feb 6, 2015 at 8:18

2 Answers 2

4

Delete storage/framework/services.json file.

Laravel cache all service providers data in this file. Always look at this file when debugging service providers.

Here is very useful answer about service providers and services.json file

How does Laravel 4 load deferred provider?

Sign up to request clarification or add additional context in comments.

Comments

1

./artisan clear-compiled also does the trick. After finding this solution I thought there must be an artisan command for this. And bingo, there already is. Taking a look at it's code I saw that besides clearing the compiled classes file it also clears services.json.

public function fire()
{
    if (file_exists($path = $this->laravel->storagePath().'/framework/compiled.php'))
    {
        @unlink($path);
    }

    if (file_exists($path = $this->laravel->storagePath().'/framework/services.json'))
    {
        @unlink($path);
    }
}

In the next bootstrapping of the app services.json is recreated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.