PHP code example of jojomak13 / laravel-attributes

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

    

jojomak13 / laravel-attributes example snippets




namespace App\Attributes;

use Attribute;
use Illuminate\Support\Facades\Gate;
use Joseph\Attributes\Concerns\ICustomAttribute;

#[Attribute(Attribute::TARGET_METHOD)]
class PolicyAttribute implements ICustomAttribute
{
    public function __construct(string $ability, $arguments = [])
    {
        $this->ability = $ability;
        $this->arguments = $arguments;
    }

    public function handle(string $ability, $arguments = [])
    {
        Gate::authorize($ability, $arguments);
    }
}



namespace App\Http\Controllers;

use App\Attributes\PolicyAttribute;
use App\Models\Post;
use Illuminate\Routing\Controller;
use Joseph\Attributes\Traits\HasAttributes;

class PostController extends Controller
{
    use HasAttributes;

    #[PolicyAttribute('viewAny', Post::class)]
    public function index()
    {
        return 'posts here';
    }
}
bash
php artisan make:attribute PolicyAttribute