php 添加有关auth用户登录Laravel的信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 添加有关auth用户登录Laravel的信息相关的知识,希望对你有一定的参考价值。

<?php

namespace App\Services\Foundation\Log;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;

/**
 * use it in ServiceProvider
 * $this->app->make('log')->getMonolog()->pushProcessor(new LaravelUserProcessor($this->app));
 */
class LaravelUserProcessor
{
    /**
     * @var Application
     */
    protected $app;

    /**
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    /**
     * @param array $record
     *
     * @return array
     */
    public function __invoke(array $record)
    {
        $user = $this->app->make(Authenticatable::class);

        if (null === $user) {
            $record['extra']['user'] = ($this->app->runningInConsole() ? 'cli' : 'guest');
        } else {
            $record['extra']['user']['id'] = $user->getAuthIdentifier();
            $record['extra']['user']['data'] = $this->getUserData($user);
        }

        return $record;
    }

    /**
     * Get information about user object
     *
     * @param $user
     * @return string
     */
    protected function getUserData($user)
    {
        if ($user instanceof Jsonable) {
            return $user->toJson();
        }
        if ($user instanceof Arrayable) {
            return json_encode($user->toArray());
        }
        return serialize($user);
    }
}

以上是关于php 添加有关auth用户登录Laravel的信息的主要内容,如果未能解决你的问题,请参考以下文章

Laravel:添加make:auth后在我的登录/注册页面中出错

视图中的 Laravel Multi-Auth @auth 语法

Laravel auth登录之怎么通过guard流程

TODO:Laravel 内置简单登录

Laravel: 使用isAdmin列检查用户是否是管理员?

用户登录后 Laravel 5 会话不会持续存在