Php Can';无法使用Laravel 5.1将用户上下文传递到Sentry

Php Can';无法使用Laravel 5.1将用户上下文传递到Sentry,php,laravel,exception,middleware,sentry,Php,Laravel,Exception,Middleware,Sentry,我的目标是将用户上下文(如电子邮件或ID)传递到Sentry,这样我就可以看到哪些用户破坏了某些东西 我已经配置了一个全局中间件,将用户上下文添加到我的Sentry错误中。下面是课堂: class AddUserToSentry { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure

我的目标是将用户上下文(如电子邮件或ID)传递到Sentry,这样我就可以看到哪些用户破坏了某些东西

我已经配置了一个全局中间件,将用户上下文添加到我的Sentry错误中。下面是课堂:

        class AddUserToSentry
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        if(Auth::user())
        {
            //dd(Auth::user()->email);
            app('sentry')->user_context(array(
                'email' => Auth::user()->email
            ));
        }
        return $next($request);
    }
}
在my Handler.php中,我有:

 /**
 * Report or log an exception.
 *
 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
 *
 * @param  \Exception  $e
 * @return void
 */
public function report(Exception $e)
{

    if ($this->shouldReport($e)) {
        app('sentry')->captureException($e);

    }
    parent::report($e);
}
我错过了什么使这项工作?对于用户上下文,我得到的只是IP地址,这在我的例子中不是很有用

多谢各位


Josh

您需要在控制器中提供对Laravel的Auth Facade的访问,如下所示:


使用Auth

这是一个完整的示例,来自官方哨兵文档。避免添加
使用Auth
您只需使用
auth()
helper函数即可

namespace App\Http\Middleware;

use Closure;

class SentryContext
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (app()->bound('sentry')) {
            /** @var \Raven_Client $sentry */
            $sentry = app('sentry');

            // Add user context
            if (auth()->check()) {
                $sentry->user_context(['id' => auth()->user()->id, 'email' => auth()->user()->email]);
            }

            // Add tags context
            // $sentry->tags_context(['foo' => 'bar']);
        }

        return $next($request);
    }
}