[[toc]]
Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering your application.
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()
}
}
go run . artisan make:middleware Auth
// Support nested folders
go run . artisan make:middleware user/Auth
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(),
}
}
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)
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()