GoravelGoravel
Home
Video
  • English
  • 简体中文
GitHub
Home
Video
  • English
  • 简体中文
GitHub
  • Prologue

    • Upgrade Guide

      • Upgrading To v1.15 From v1.14
      • Upgrading To v1.14 From v1.13
      • History Upgrade
    • Contribution Guide
    • Excellent Extend Packages
  • Getting Started

    • Installation
    • Configuration
    • Directory Structure
    • Compile
  • Architecture Concepts

    • Request Lifecycle
    • Service Container
    • Service Providers
    • Facades
  • The Basics

    • Routing
    • HTTP Middleware
    • Controllers
    • Requests
    • Responses
    • Views
    • Grpc
    • Session
    • Validation
    • Logging
  • Digging Deeper

    • Artisan Console
    • Cache
    • Events
    • File Storage
    • Mail
    • Queues
    • Task Scheduling
    • Localization
    • Package Development
    • Color
    • Strings
    • Helpers
  • Security

    • Authentication
    • Authorization
    • Encryption
    • Hashing
  • ORM

    • Getting Started
    • Relationships
    • Migrations
    • Seeding
    • Factories
  • Testing

    • Getting Started
    • HTTP Tests
    • Mock

Middleware

  • Introduction
  • Define Middleware
    • Create Middleware By Command
  • Register Middleware
    • Global Middleware
    • Assign Middleware for Routing
  • Abort Request

Introduction

Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering your application.

Define Middleware

You can create your own middleware in the app/http/middleware directory, the structure is as follows.

package middleware

import (
  "github.com/goravel/framework/contracts/http"
)

func Auth() http.Middleware {
  return func(ctx http.Context) {
    ctx.Request().Next()
  }
}

Create Middleware By Command

go run . artisan make:middleware Auth

// Support nested folders
go run . artisan make:middleware user/Auth

Register Middleware

Global Middleware

If you want to apply middleware for every HTTP request of your application, you only need to register the middleware in the Middleware in the app/http/kernel.go file.

// app/http/kernel.go
package http

import (
  "github.com/goravel/framework/contracts/http"
  
  "goravel/app/http/middleware"
)

type Kernel struct {
}

func (kernel *Kernel) Middleware() []http.Middleware {
  return []http.Middleware{
    middleware.Auth(),
  }
}

Assign Middleware for Routing

You can register the middleware for some routing separately:

import "github.com/goravel/framework/http/middleware"

facades.Route().Middleware(middleware.Auth()).Get("users", userController.Show)

Abort Request

In middleware, if you need to interrupt the request, you can use the Abort method.

ctx.Request().Abort()
ctx.Request().Abort(http.StatusNotFound)
ctx.Response().String(http.StatusNotFound, "Not Found").Abort()
Edit this page
Prev
Routing
Next
Controllers