Home >Backend Development >PHP Tutorial >Laravel Log模块设计

Laravel Log模块设计

WBOY
WBOYOriginal
2016-06-06 20:13:53932browse

写一个laravel项目,需求是将CURD的记录写入mysql的log表中,
我现在的方案就是CURD结束后自己写的LOG repository 记录一下 ,如下,

<code>$result = $this->customer->add($request->all());
if($result>0){
    $this->log->write('新增客户'.$request->input('name'));
}</code>

觉得很不优雅,大家有什么好的思路吗

回复内容:

写一个laravel项目,需求是将CURD的记录写入mysql的log表中,
我现在的方案就是CURD结束后自己写的LOG repository 记录一下 ,如下,

<code>$result = $this->customer->add($request->all());
if($result>0){
    $this->log->write('新增客户'.$request->input('name'));
}</code>

觉得很不优雅,大家有什么好的思路吗

用laravel 的事件机制记录日志
http://www.golaravel.com/laravel/docs/5.0/events/

注册“新增客户”事件

'App\Events\AddUser'

注册“日志记录”事件监听者

'App\Listeners\LogRecord'

订阅事件

在 EventServiceProvider 中

protected $listen = [

<code>'App\Events\AddUser' => [
    'App\Listeners\LogRecord',
],</code>

];

触发事件

$result = $this->customer->add($request->all());
event(new AddUser($result ));

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn