Exact place to register observer in Laravel 4

时光总嘲笑我的痴心妄想 提交于 2019-12-03 18:42:19

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/24772379/exact-place-to-register-observer-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!