在 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
Exact place to register observer in Laravel 4
提问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);
但我不确定最好的地方在哪里。
回答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 boot
method of the model itself.
由于观察者只是您正在收听的事件的集合,我会说将它放在 Laravel 建议您放置单个事件的地方:在boot
模型本身的方法上。
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.
将UserObserver
类放在哪里更灵活,取决于它最终的复杂程度。
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
:
如果您可以在每次应用程序运行时加载它,请创建一个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.php
class 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.json
and 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-autoload
after editing composer.json
.
PS:composer dump-autoload
编辑完后别忘了运行composer.json
。