swinburne / laravel-session-started-event

当Laravel中开始会话时触发一个事件,用于在服务提供者中使用

v1.0.0 2017-06-28 14:29 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:08:55 UTC


README

本包触发一个事件,可用于服务提供者(或任何应用场景)在会话启动时执行代码。

安装

将服务提供者添加到 config/app.php 文件中的 providers 数组。

'providers' => [
    ...
    Swinburne\LaravelSessionStarted\SessionStartedServiceProvider::class,
    ...
],

app/Http/Kernel.php 中的现有 Laravel StartSession 中间件替换为本包中的中间件。默认情况下,它在 web 组中。

替换

\Illuminate\Session\Middleware\StartSession::class

\Swinburne\LaravelSessionStarted\Http\Middleware\StartSession::class

例如

protected $middlewareGroups = [
    'web' => [
        ...
        \Swinburne\LaravelSessionStarted\Http\Middleware\StartSession::class,
        ...
    ]
    ...

使用方法

安装完成后,您可能需要监听以下事件,该事件在会话启动时触发。

\Swinburne\LaravelSessionStarted\Events\SessionStarted

例如,在服务提供者的 boot() 方法中,您可以按照以下方式监听事件。

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Swinburne\LaravelSessionStarted\Events\SessionStarted;

class AppServiceProvider extends ServiceProvider
{
    /**
    * Boot the application services.
    *
    * @return void
    */
    public function boot()
    {
        Event::listen(SessionStarted::class, function ($event) {
            echo "My session was started";
        });
    }
}