PHP code example of kylearch / ephemeral-users

1. Go to this page and download the library: Download kylearch/ephemeral-users library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kylearch / ephemeral-users example snippets


use KyleArch\EphemeralUsers\Contracts\EphemeralUser as EphemeralUserContract;
use KyleArch\EphemeralUsers\Concerns\HasEphemeralState;

class User extends Authenticatable implements EphemeralUserContract
{
    use HasEphemeralState;

    // Your existing User model code...
}

// Create an ephemeral user instance
$ephemeralUser = User::ephemeral([
    'id' => 'session-abc123',
    'email' => '[email protected]',
    'name' => 'Anonymous User',
]);

// Check if a user is ephemeral
if ($ephemeralUser->isEphemeral()) {
    // Handle ephemeral user logic
}

// Get the ephemeral identifier
$identifier = $ephemeralUser->getEphemeralIdentifier(); // 'session-abc123'

$ephemeralUser = User::ephemeral(['id' => 'test']);

try {
    $ephemeralUser->save(); // Throws EphemeralPersistenceException
} catch (EphemeralPersistenceException $e) {
    // Handle the exception
    $user = $e->getEphemeralUser();
}

return [
    // Throw exception on persist attempts (default: true)
    'throw_on_persist' => env('EPHEMERAL_THROW_ON_PERSIST', true),

    // Log persist attempts (default: true)
    'log_attempts' => env('EPHEMERAL_LOG_PERSIST', true),

    // Log channel to use
    'log_channel' => env('EPHEMERAL_LOG_CHANNEL', 'stack'),

    // Log level for persist attempts
    'log_level' => env('EPHEMERAL_LOG_LEVEL', 'warning'),
];
bash
php artisan vendor:publish --tag=ephemeral-users-config