PHP code example of laracomponents / impersonation

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

    

laracomponents / impersonation example snippets


use LaraComponents\Impersonation\Traits\Impersonable;

class User
{
    use Impersonable;
    ...

    /**
     * Optional method
     * Default return the "impersonate_id"
    **/
    public function getImpersonatingKey()
    {
        return 'your session key here';
    }
}

    protected $middlewareGroups = [
        'web' => [
            ...
            \LaraComponents\Impersonation\Middleware\CheckForImpersonating::class,
        ],
        ...
    ];

Route::get('users/{id}/impersonate', function ($id) {
    $user = \App\User::findOrFail($id);

    if(! $user->isImpersonating()) {
        $user->impersonate();
    }

    return redirect('/');
});

Route::get('users/unimpersonate', function () {
    $user = \Auth::user();

    if($user->isImpersonating()) {
        $user->unimpersonate();
    }

    return redirect('/');
});