PHP code example of banityt / laravel-impersonate

1. Go to this page and download the library: Download banityt/laravel-impersonate 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/ */

    

banityt / laravel-impersonate example snippets


use BaniTo\Impersonate\Contracts\Impersonatable as ImpersonatableContract;
use BaniTo\Impersonate\Traits\Impersonatable as ImpersonatableTrait;

class User extends Authenticatable implements ImpersonatableContract
{
    use ImpersonatableTrait;
    
    //...
}

public function guardName()
{
    return 'web';   // or any custom guard name specified in config/auth.php
}

public function canImpersonate() : bool
{
    return Bouncer::can('users_impersonate');
}

public function canBeImpersonated() : bool
{
    return !Bouncer::is('admin');
}

use Impersonate;

// Impersonate other user, can be another user with other guard
auth()->user()->impersonate($anotherUser);
Auth::user()->impersonate($otherGuardUser);
Impersonate::impersonate($impersonator, $beingImpersonated);

// Stop impersonating
auth()->user()->stopImpersonating();
Auth::user()->stopImpersonating();
Impersonate::stopImpersonating();

auth()->user()->isImpersonating();
Auth::user()->isImpersonating();
Impersonate::isImpersonating();

// blade example
@if (Impersonate::isImpersonating())
    <a href="{{route('your.route.impersonate.stop')}}">{{__('Stop Impersonation')}}</a>
@endif