Projectslaravel-impersonationConfiguration

Laravel Impersonation

Package

Session-based user impersonation for Laravel.

Upgrade Guide

Configuration

Publish the config file:

php artisan vendor:publish --tag=impersonation-config

Options

KeyDefaultPurpose
session_keyimpersonation_dataSession key for the impersonation stack
guardnullAuth guard for default user/login handlers (null = default guard)
max_depth1Maximum nested impersonation depth
routing.enabledfalseRegister package start/stop routes
routing.prefiximpersonationURL prefix for those routes
routing.middleware['web', 'auth']Middleware applied to package routes
routing.names.startimpersonation.startRoute name for start
routing.names.stopimpersonation.stopRoute name for stop
routing.impersonatee_modelApp\Models\UserModel resolved from the {impersonatee} route parameter

Depth

// config/impersonation.php
'max_depth' => 1, // Admin → User only
'max_depth' => 2, // Admin → User → Another user

When the stack is at max_depth, canImpersonate() returns false.

Guard

'guard' => 'web',

Default user and login handlers use Auth::guard($guard) (or the default guard when null).

Runtime overrides

ImpersonationConfig::set() and ImpersonationConfig::clearCache() are available in tests. Prefer published config (or config([...]) before the app boots) in apps.

Continue to Usage.