Exact place to register observer in Laravel 4

前端 未结 1 2005
后悔当初
后悔当初 2020-12-25 09:06

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);

相关标签:
1条回答
  • 2020-12-25 09:29

    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.

    0 讨论(0)
提交回复
热议问题