Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exact place to register observer in Laravel 4

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

like image 960
Gowtham Selvaraj Avatar asked Jul 16 '14 04:07

Gowtham Selvaraj


People also ask

How do you call observer in laravel?

Model::create([]); //this triggers the creating method first, then created method in the observer class. Updating — This observer method is called when a model record is in the updating process, at this point, the updates has not yet been persisted to the database.

What are the parameters to an observer?

The first parameter is the observer object. The second parameter is the observed event type. The remaining parameters are optional observed event binding types. The observer is notified when an event object that is assignable to the observed event type is raised with the observed event binding types.


1 Answers

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.

like image 113
Tomas Buteler Avatar answered Oct 08 '22 13:10

Tomas Buteler