PHPackages                             scif/laravel-pretend - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. scif/laravel-pretend

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

scif/laravel-pretend
====================

Symfony style impersonation for the Laravel Framework.

v1.1.2(6y ago)5125MITPHPPHP &gt;=7.0CI failing

Since Mar 25Pushed 6y ago1 watchersCompare

[ Source](https://github.com/SCIF/laravel-pretend)[ Packagist](https://packagist.org/packages/scif/laravel-pretend)[ RSS](/packages/scif-laravel-pretend/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (5)Versions (6)Used By (0)

[![Code Coverage](https://camo.githubusercontent.com/5dbae0b8f358274d687c16095fb1e3108f61af85959710bdd694b449e4ae6458/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f534349462f6c61726176656c2d70726574656e642f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/SCIF/laravel-pretend/?branch=master)[![Build Status](https://camo.githubusercontent.com/efbbe75cd304f9fa8f2cad1a49290cbfc83b40db9113c2e9867b80ca29fce685/68747470733a2f2f7472617669732d63692e6f72672f534349462f6c61726176656c2d70726574656e642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/SCIF/laravel-pretend)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/9a071f05fe44ddf4e7abdc93e14aa51b70f826246357bb8a83cf664f14ccf1d7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f534349462f6c61726176656c2d70726574656e642f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/SCIF/laravel-pretend/?branch=master)

Impersonate package for the Laravel Framework
=============================================

[](#impersonate-package-for-the-laravel-framework)

[На русском](README_RU.md)

What is that?
-------------

[](#what-is-that)

Laravel has no default impersonation wrapper for low-level methods. This package highly inspired by [Symfony impersonation](http://symfony.com/doc/current/security/impersonating_user.html) which looks much more flexible rather than several inspected Laravel implementations. Package fully implement GET-parameter-driven behavior. Also, this package does not restrict you in using custom [user providers](https://laravel.com/docs/master/authentication#the-user-provider-contract) (for instance, if you use [Propel](https://github.com/propelorm/PropelLaravel)), guards and can be used with [Twig](https://github.com/rcrowe/TwigBridge) as view templater. Some ideas inspired by existing impersonation packages for Laravel (:+1: thanks to those authors!).

Installation
------------

[](#installation)

```
composer require scif/laravel-pretend

```

Add service provider to `config/app.php` after Laravel service providers but before your own:

```
    'providers' => [
       …
        Scif\LaravelPretend\LaravelPretendServiceProvider::class,
        /*
         * Application Service Providers...
         */
         …
     ]
```

Add middleware handling impersonation:

- to your `Kernel` class: ```
    protected $middlewareGroups = [
        'web' => [
            …
            Scif\LaravelPretend\Middleware\Impersonate::class,
        ],
    ```

    This way is most common and covers all cases I can assume.
- or by [any suitable methods](https://laravel.com/docs/5.4/middleware#registering-middleware) for some especial cases.

The latest step of installation is a configuring authorization [gate](https://laravel.com/docs/5.3/authorization#gates). Package bundled with gate called `impersonate`. This gate checks if your user model implements `Scif\LaravelPretend\Interfaces\Impersonable` and check method `canImpersonate(): bool`.

So your model can looks like:

```
class User extends Authenticatable implements Impersonable
{
…

    public function canImpersonate(): bool
    {
        return $this->isAdmin();
    }
}
```

☝️ You can use out of box implementation of this gate or override it in your own AuthServiceProvider. You can override name of gate used to check permissions in configuration as well.

Configuration
-------------

[](#configuration)

Configuration file can be easily copied to your project by `vendor:publish` command:

```
php ./artisan  vendor:publish --provider=Scif\\LaravelPretend\\LaravelPretendServiceProvider --tag=config
```

Configuration consist of just two options:

```
return [
    'impersonate' => [
        'user_identifier' => 'email',
        'auth_check' => 'impersonate',
    ]
];
```

- `user_identifier` — this string will be used as name of field using to retrieve user object from user provider (method `retrieveByCredentials()`). The default value `email` makes your impersonation urls beauty: `?_switch_user=admin@site.com` is much clear rather than `?_switch_user=43`. But it's up to you
- `auth_check` — this string is a name of [Gate](https://laravel.com/docs/5.4/authorization#gates) used to check ability of user to impersonate. In fact the default Gate could be easily overriden in `AuthServiceProvider` of your application.

Usage
-----

[](#usage)

As mentioned above, this package repeats Symfony style of using GET-parameters to manage impersonation.

Blade using is pretty straightforward:

```
// generates link with impersonation
{{ route('home', ['_switch_user' => 'admin@site.com']) }}

// exit impersonation
    @if ($app['impersonator']->isImpersonated())
        Exit impersonation
    @else
        Logout
    @endif
```

And here is a simple example using in twig:

```
// generates link with impersonation
{{ route('home', {'_switch_user': 'admin@site.com'}) }}

// more advance usage
                {% if auth_user() %}
                    {% if app.impersonator.impersonated %}
                        Exit impersonation
                    {% else %}

                            {{ csrf_field() }}
                            Logout

                    {% endif %}
                {% endif %}

```

Events
------

[](#events)

On entering and exitting impersontaion this package raises events: [`Scif\LaravelPretend\Event\Impersontated`](src/Event/Impersonated.php), [`Scif\LaravelPretend\Event\Unimpersontated`](src/Event/Unimpersonated.php). Name of events is their fully qualified class names, so simplest event listener will looks like:

```
use Scif\LaravelPretend\Event\Impersontated;
…
    Event::listen(Impersonated::class, function (Impersonated $event) {
        //
    });
```

Forbid impersonation
--------------------

[](#forbid-impersonation)

You can use bundled [`ForbidImpersonation`](src/Middleware/ForbidImpersonation.php) middleware to forbid using of impersonation for some route groups, routes or controllers.

PHP7? Ugh! Wtf??
----------------

[](#php7-ugh-wtf)

Yes, PHP7 is awesome! So, if you want to use it with PHP5 — [create an issue](https://github.com/SCIF/laravel-pretend/issues) and I will think about a separate branch or other suitable solution.

Contribution
------------

[](#contribution)

Any type of contributions is highly appreciated. Don't be a shy — help this project become even better!

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~276 days

Total

5

Last Release

2278d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a7dc66e4d5a2c28386f6c96323362fe391b4cbcbf69919573d4a08f5b3ce17c7?d=identicon)[SCIF](/maintainers/SCIF)

---

Top Contributors

[![SCIF](https://avatars.githubusercontent.com/u/671925?v=4)](https://github.com/SCIF "SCIF (34 commits)")

---

Tags

laravelimpersonatesymfony style

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/scif-laravel-pretend/health.svg)

```
[![Health](https://phpackages.com/badges/scif-laravel-pretend/health.svg)](https://phpackages.com/packages/scif-laravel-pretend)
```

###  Alternatives

[lab404/laravel-impersonate

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

2.3k17.8M59](/packages/lab404-laravel-impersonate)[jeremy379/laravel-openid-connect

OpenID Connect support to the PHP League's OAuth2 Server. Compatible with Laravel Passport.

59403.6k8](/packages/jeremy379-laravel-openid-connect)[rickycezar/laravel-jwt-impersonate

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

24122.8k](/packages/rickycezar-laravel-jwt-impersonate)[aurorawebsoftware/aauth

Laravel Aauth

412.1k1](/packages/aurorawebsoftware-aauth)[hapidjus/laravel-impersonate-ui

UI for 404labfr/laravel-impersonate

371.5k](/packages/hapidjus-laravel-impersonate-ui)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
