1. Go to this page and download the library: Download andrewdyer/auth-gate 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/ */
andrewdyer / auth-gate example snippets
use AndrewDyer\Gate\Contracts\Authenticatable;
class User implements Authenticatable
{
public function __construct(
public readonly int $id,
public readonly bool $admin = false,
) {}
public function isAdmin(): bool
{
return $this->admin;
}
}
use AndrewDyer\Gate\Gate;
$actor = new User(id: 1);
$gate = new Gate($actor);
$gate->define('edit-post', function ($actor, $post) {
return $actor->id === $post->authorId;
});
$gate->allows('edit-post', $post); // true or false
$gate->denies('edit-post', $post); // true or false
$gate->all(['edit-post', 'delete-post'], $post); // true if all pass
$gate->any(['edit-post', 'view-post'], $post); // true if any pass
use AndrewDyer\Gate\UnauthorizedException;
try {
$gate->authorize(['edit-post'], $post);
} catch (UnauthorizedException $e) {
// Actor is not authorised
}