في الإصدارات السابقة، كنا نطبق middleware بداخل controller من خلال إستخدام __construct()
class CourseController extends Controller
{
public function __construct()
{
$this->middleware('permission:courses', ['only' => ['index']]);
$this->middleware('permission:add_course', ['only' => ['create', 'store']]);
$this->middleware('permission:edit_course', ['only' => ['edit', 'update', 'updateStatus']]);
$this->middleware('permission:delete_course', ['only' => ['destroy']]);
}
}
بينما في Laravel 11 نحتاج لإستخدام دالة middleware وكذلك ان يجب عمل implements hasMiddleware
class UserController extends Controller implements hasMiddleware
{
public static function middleware(): array
{
return [
new Middleware('permission:users', only: ['index']),
new Middleware('add_course', only: ['only' => ['index']]),
];
}
}