在 Laravel 4 中注册观察者的确切位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24772379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:49:13  来源:igfitidea点击:

Exact place to register observer in Laravel 4

laravelmodelobservers

提问by Gowtham Selvaraj

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.

当为模型观察者使用单独的类时,我应该在哪里注册观察者?文档说要打电话,User::observe(new UserObserver);但我不确定最好的地方在哪里。

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

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

回答by Tomas Buteler

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 bootmethod of the model itself.

由于观察者只是您正在收听的事件的集合,我会说将它放在 Laravel 建议您放置单个事件的地方:在boot模型本身的方法上。

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

        User::observe(new UserObserver);
    }
}

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

UserObserver类放在哪里更灵活,取决于它最终的复杂程度。

For simple observers

对于简单的观察者

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

如果您可以在每次应用程序运行时加载它,请创建一个app/observers.php文件,然后将其放在您的末尾app/start/global.php

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

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

或者,您可以使用 composer 自动加载该文件,方法是附加您的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).

如果你打算有很多不同的观察者,我会说创建你自己的命名空间,让 Laravel / Composer 为你自动加载。为了做到这一点,创建一个文件夹app/MyNamespace/Observers,然后将每个观察者文件放入其中(每个文件的名称都与类完全相同——即UserObserver.php)。

Your UserObserver.phpclass should now look like this:

你的UserObserver.php类现在应该是这样的:

<?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.jsonand add your namespace to follow PSR-0 autoloading:

最后,编辑您composer.json的命名空间并添加您的命名空间以遵循 PSR-0 自动加载:

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

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

PS:composer dump-autoload编辑完后别忘了运行composer.json