在Laravel 4中注册观察员的确切地点 [英] Exact place to register observer in Laravel 4

查看:62
本文介绍了在Laravel 4中注册观察员的确切地点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为模型观察者使用单独的类时,应该在哪里确切地注册观察者?该文档说要致电User::observe(new UserObserver);,但我不确定执行此操作的最佳位置在哪里.

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

推荐答案

由于观察者只是您正在收听的事件的集合,因此我想将其放置在Laravel建议您放置各个事件的位置:在boot模型本身的方法.

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

放置UserObserver类的位置要灵活一些,并取决于最终的复杂程度.

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

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

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

或者,您可以通过添加composer.json来使用composer自动加载该文件:

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

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

对于更复杂的观察者

如果您打算有许多不同的观察者,那么我想说说创建您自己的命名空间,然后让Laravel/Composer为您执行自动加载.为此,请创建一个类似于app/MyNamespace/Observers的文件夹,然后将每个观察者文件放入其中(每个观察者文件的名称都与类完全相同,即UserObserver.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).

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

或者:

use MyNamespace\Observers\UserObserver;

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

        User::observe(new UserObserver);
    }
}

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

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

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

PS:不要忘记在编辑composer.json之后运行composer dump-autoload.

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

这篇关于在Laravel 4中注册观察员的确切地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆