Laravel 5.3:如何在全球中间件中使用Check Auth?

时间:2017-06-14 07:37:36

标签: laravel authentication middleware

我希望在我的网站标题中显示未读邮件的数量以及其他一些视图。为此,我写了一个全球中间件,但是这个中间件无法访问已注册用户的身份验证信息。

<?php

namespace PTA_OIMS\Http\Middleware;

use Illuminate\View\Factory;
use Closure;
use Illuminate\Contracts\Auth\Guard;
use PTA_OIMS\Kartable;
use Session;

class UnreadMessage
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */

    protected $auth;
    protected $view;

    public function __construct(Guard $auth, Factory $view)
    {
        $this->auth = $auth;
        $this->view = $view;
    }

    public function handle($request, Closure $next)
    {
        $unreadMessage = NULL;
        $user = $this->auth->check();
        if (!empty($user)) {
            $unreadMessage = Kartable::
            where('id_reciver', '=',
                Session::get('personnel_info.0')->box_id)
                ->whereBetween('status', [1, 2])
                ->where('view_date', '=', Null)
                ->count();
        }
        $this->view->share('unreadMessage', $unreadMessage);
        return $next($request);
    }
}

更新

<?php

namespace PTA_OIMS\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \PTA_OIMS\Http\Middleware\UnreadMessage::class,

    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \PTA_OIMS\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \PTA_OIMS\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \PTA_OIMS\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

    ];
}

1 个答案:

答案 0 :(得分:0)

尝试使用全局auth()帮助程序:

$user = auth()->check();