12

When using a separate class for a model observer, where exactly should I register the observer? The documentation says to call User::observe(new UserObserver); but I’m not sure where the best place to do this would be.

https://laravel.com/docs/5.4/eloquent#observers

Rodia
  • 1,407
  • 8
  • 22
  • 29
Gowtham Selvaraj
  • 478
  • 1
  • 5
  • 12

1 Answers1

22

Since an observer is just a collection of events you are listening to, I'd say place it where Laravel suggests you place individual events: on the boot method of the model itself.

class User extends Eloquent
{
    public static function boot()
    {
        parent::boot();

        User::observe(new UserObserver);
    }
}

Where to put the UserObserver class is a little more flexible and depends on how complex it will end up being.

For simple observers

If you can bare having it load every time the app runs, create an app/observers.php file, then put this at the end of your app/start/global.php:

require app_path().'/observers.php';

Alternatively, you can use composer to autoload that one file, by appending your composer.json:

{
    "autoload": {
        "classmap": [
            //...
        ],
        "files": [
            "app/observers.php"
        ]
    }
}

For more complex observers

If you plan to have many different observers, I'd say create your own namespace and let Laravel / Composer do the autoloading for you. In order to do that, create a folder like app/MyNamespace/Observers, then place each observer file inside of it (each named exactly like the class -- i.e. UserObserver.php).

Your UserObserver.php class should now look like this:

<?php

namespace MyNamespace\Observers;

class UserObserver
{
    public function saving($model)
    {
        // ...
    }

    public function saved($model)
    {
        // ...
    }
}

And you'll have to declare the full class whenever you are using it:

User::observe(new MyNamespace\Observers\UserObserver);

Or:

use MyNamespace\Observers\UserObserver;

class User extends Eloquent
{    
    public static function boot()
    {
        parent::boot();

        User::observe(new UserObserver);
    }
}

Finally, edit your composer.json and add your namespace to follow PSR-0 autoloading:

{
    "autoload": {
        "classmap": [
            //...
        ],
        "psr-0": [
            "MyNamespace": "app/"
        ]
    }
}

PS: Don't forget to run composer dump-autoload after editing composer.json.

Tomas Buteler
  • 3,892
  • 4
  • 29
  • 42