What are the potential performance implications of checking user login and permissions in each controller method?

Checking user login and permissions in each controller method can lead to redundant code and decreased performance due to the repetitive checks. To solve this issue, a middleware can be implemented to handle user authentication and permissions before the request reaches the controller method. This way, the checks are centralized and only need to be performed once per request.

// Middleware for user authentication and permissions
class AuthMiddleware
{
    public function handle($request, Closure $next)
    {
        // Check user authentication and permissions here
        
        return $next($request);
    }
}

// Implementing middleware in controller
class ExampleController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    
    public function index()
    {
        // Controller method logic here
    }
}