PHP 8.0 release was a revolution for the language.
It brings cool features like Named arguments
, Attributes
, Constructor property
and ...
PHP 8.1 brings even more exciting features like Enumerations
, New in initializers
, Array unpacking
and ...
The idea of this package is using PHP Attribute
in a laravel project.
composer require saeedpooyanfar/laravel-annotation
In App\Http\Controllers\Controller::class
:
Replace use Illuminate\Routing\Controller as BaseController;
With use LaravelAnnotation\BaseController;
Or if you don't want to change your BaseController you can:
Use LaravelAnnotation\AttributeMiddleware
trait
In App\Http\Controllers\Controller::class
Here is an example that how you can use Middleware
attribute in a laravel controller:
<?php
use LaravelAnnotation\Attribute\Middleware;
#[Middleware(RedirectIfAuthenticated::class, 'sanctum', except: 'logout')]
class AuthController extends Controller
{
public function register()
{
}
public function login()
{
}
#[Middleware(Authenticate::class, 'sanctum')]
public function logout()
{
}
}