HTTP Routes
Package routes are disabled by default. Enable them in config when you want built-in start/stop endpoints.
Enable
// config/impersonation.php
'routing' => [
'enabled' => true,
'prefix' => 'impersonation',
'middleware' => ['web', 'auth'],
'names' => [
'start' => 'impersonation.start',
'stop' => 'impersonation.stop',
],
'impersonatee_model' => App\Models\User::class,
],
Endpoints
| Method | Path | Name | Action |
|---|---|---|---|
POST | /{prefix}/start/{impersonatee} | impersonation.start | Start impersonating |
POST | /{prefix}/stop | impersonation.stop | Stop one level |
Default paths: /impersonation/start/{impersonatee} and /impersonation/stop.
The {impersonatee} value is resolved via resolveImpersonatee (default:
impersonatee_model::findOrFail()).
Responses
By default:
- JSON requests →
{ "success": true } - Other requests →
redirect()->back()
Customize via configure():
ImpersonationManager::configure(
authorize: fn () => true,
startResponse: fn ($request, $manager) => redirect('/dashboard'),
stopResponse: fn ($request, $manager) => redirect('/admin'),
);
Your own controllers
Keep routing.enabled as false and call the manager from your own routes:
Route::post('/admin/users/{user}/impersonate', function (User $user) {
ImpersonationManager::make()->impersonate($user);
return redirect('/dashboard');
})->middleware(['web', 'auth']);
See Middleware for route guards while impersonating.