在laravel中记录用户操作 [英] Logging user actions in laravel

查看:475
本文介绍了在laravel中记录用户操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户执行的所有操作(登录/注销/CRUD)记录到数据库中的日志表中,从我所看到的情况来看,事件似乎是执行此操作的正确方法.

I'm trying to log all actions that users do (login / logout / CRUD) to a logs table in my database, and from what I've seen events look to be the right way to do this.

我在User模型中添加了did($action)方法,该方法将给定用户的操作记录到数据库中.

I've added a did($action) method to the User model, which logs an action to the database for the given user.

这是到目前为止我得到的:

Here's what I've got so far:

EventServiceProvider.php

EventServiceProvider.php

namespace App\Events;

use Illuminate\Support\ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->events->subscribe(new UserEventSubscriber);
    }
}

UserEventSubscriber.php

UserEventSubscriber.php

namespace App\Events;

class UserEventSubscriber
{
    public function login(\User $user)
    {
        return $user->did('logged_in');
    }

    public function logout(\User $user)
    {
        return $user->did('logged_out');
    }

    public function subscribe($events)
    {
        $events->listen('user.login', 'App\Events\UserEventSubscriber@login');

        $events->listen('user.logout', 'App\Events\UserEventSubscriber@logout');
    }
}

要记录操作:

Event::fire('user.logout', array(Auth::user()));

我仍在努力将注意力集中在服务提供商周围,因此我在这里可能会很落伍.

I'm still trying to wrap my mind around service providers, so I could be very off-base here.

我的问题:

1)服务提供者是正确的选择吗?

1) Is a service provider the right thing to use or this?

2)是否有更好的方法,不需要每次都手动将Auth::user()传递给事件?

2) Is there a better approach that doesn't require manually passing Auth::user() to the event every time?

3)应该在什么级别触发事件?我会尽可能地使用模型,因为它将通过批量操作提供更多有用的日志.否则会有控制器或存储库.

3) At what level should events be fired? I'm leaning towards model whenever possible, as it would provide more useful logs from bulk actions. Otherwise there's the controller or repository.

4)这些事件仅在管理区域(/admin/*)中是必需的.以某种方式将其限制为仅在网站的该部分是否有益?

4) These events are only necessary in the admin area (/admin/*). Would it be beneficial to somehow restrict this to only that section of the site?

5)我有关记录用户操作的搜索非常不成功.这只是开发人员不做的事情吗?如果是这样,他们会做什么 ?

5) My searches regarding the logging of user actions have been very unfruitful. Is this just something developers don't do? If so, what do they do?

推荐答案

Is a service provider the right thing to use or this?

是的,最好使用service provider引导程序,但这不是必需的.如果您愿意,可以完全排除EventServiceProvider service provider,并可以使用以下方法从app/start/global.php文件中执行相同的操作:

Yes, it's a good idea to use a service provider for bootstraping things but not necessary. If you want you may completely exclude the EventServiceProvider service provider and can do same thing from your app/start/global.php file using this:

$app->events->subscribe(new Events\UserEventSubscriber);

由于$app是全局变量,因此您可以在此处使用它,但这不是在(global.php)文件中执行此操作的更干净的方法,但是service provider只是引导事物的一种简洁的方法(例如使用include "someClass.php"包含php文件),因为Laravel会在框架的启动过程中调用每个service provider类中定义的register方法,因此开发人员可以进行一些引导/初始化/包含等操作在应用程序分派路线之前.

Since $app is a global variable so you may use it here but it's not a cleaner way to do this in this (global.php) file but a service provider is only a neat and clean way to bootstrap things (like including php files using include "someClass.php") because the Laravel calls the register method defined in every service provider class during the bootup process of the framework so developers can do some bootstraping/initializing/including and such like things before the application dispatch the route.

有没有不需要手动传递的更好的方法 每次都对事件进行Auth :: user()吗?

Is there a better approach that doesn't require manually passing Auth::user() to the event every time?

还有其他方法,但在这种情况下,请坚持使用当前方法,因为依赖项是Auth::user(),即当前登录的用户,因此最好手动传递使用,否则您也可以像这样直接使用\Auth::user()->did() :

There are other ways but in this case stick with you current approach because dependency is the Auth::user() means that, the currently logged in user so it's better to pass the use manually or you may also use \Auth::user()->did() directly like this:

public function login()
{
    return \Auth::user()->did('logged_in');
}

这是另一种情况,但是Laravel提供了一种不错的方法,当您在__constructor类中键入强制类型转换时,可以使用IoC容器自动解决依赖关系,例如:

This is a different case but Laravel provides a nice way to automatically resolve the dependencies using the IoC container when you type cast any dependency in a __constructor class, for example:

class SomeClass {
    public function __construct(User $user)
    {
        $this->use = $user;
    }
}

在这种情况下,使用此类时无需传递User类,因为IoC容器可以在框架实例化时自动注入依赖项,但是在您的情况下,他的依赖项是Auth::user()/looged in user所以这有点不同,因此可以手动执行或直接使用Auth::user()->did().

In this case you don't need to pass the User class when you use this class because the IoC container can automatically inject the dependency when the framework instantiate it but in your case, he dependency is Auth::user()/looged in user so it's a bit different thing so manually do it or use Auth::user()->did() directly.

应该在什么级别上触发事件?我倾向于模特 只要有可能,因为它将从批量中提供更多有用的日志 动作.否则会有控制器或存储库.

At what level should events be fired? I'm leaning towards model whenever possible, as it would provide more useful logs from bulk actions. Otherwise there's the controller or repository.

没有level,这取决于您的需要和偏好,也许还取决于应用程序的体系结构.实际上,即使不使用events,您也可以构建应用程序.

There is no level for this, it depends on your need and preference and maybe architecture of the application as well. Actually, you may build your application even without using events.

这些事件仅在管理区域(/admin/*)中是必需的.会吗 有利于以某种方式将其限制为仅 网站?

These events are only necessary in the admin area (/admin/*). Would it be beneficial to somehow restrict this to only that section of the site?

也许您可以但没有必要,没什么大不了的.

Maybe you can but no need, not a very big deal IMO.

我有关记录用户操作的搜索非常 没有结果.这只是开发人员不做的事情吗?如果是这样,该怎么办 他们会吗?

My searches regarding the logging of user actions have been very unfruitful. Is this just something developers don't do? If so, what do they do?

不能完全确定您在说什么,但是如果您在谈论用户操作的logging,则答案是:depends.我曾经为一家旅行社做过一次,在他们的应用程序中,用户操作的logging非常重要,因此我记录了用户登录后几乎执行的所有操作,例如:从客户那里收到付款,卖票,他们(员工/用户)的日志in/out,以便更高权限的人员可以检查他们的活动.

Not completely sure what you are talking about but if you are talking about the logging of user actions then the answer is: depends. I did once for a travel agency, in their application the logging of user actions were very important so I logged almost everything a user does after (s)he logs in, such as: receiving a payment from a client, selling a ticket, their (employees/users) log in/out so higher authority could check their activities.

不要犹豫,其他人会做什么,找出您需要做的事情,了解您的要求并相应地进行开发.

Don't hesitate about what others do, find out what you need to do, understand your requirements and develop accordingly.

这篇关于在laravel中记录用户操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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