Download the PHP package beebmx/kirby-middleware without Composer
On this page you can find all versions of the php package beebmx/kirby-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download beebmx/kirby-middleware
More information about beebmx/kirby-middleware
Files in beebmx/kirby-middleware
Package kirby-middleware
Short Description Middleware provides a powerful mechanism for inspecting and filtering requests entering in your Kirby site.
License MIT
Informations about the package kirby-middleware
Middleware for Kirby
Middleware provides a powerful mechanism for inspecting and filtering requests entering your Kirby site.
Overview
- 1. Installation
- 2. Usage
- 3. Middleware
- 4. Options
- 5. Facades
- 6. Plugins
- 7. Roadmap
- 8. License
- 9. Credits
Installation
Download
Download and copy this repository to /site/plugins/kirby-middleware.
Composer
Usage
Out of the box, you don't need to do anything to start using (except for installation).
When you install the Middleware package, it comes with two ways of management middlewares, global middlewares and groups of middlewares.
Global middlewares
This middleware will always be triggered in every Page by the Middleware handler.
Out of the box comes with a TrimStrings middleware, which will remove spaces in the Request made.
[!NOTE] To access to this request, you should call the
Beebmx\KirbyMiddleware\Requestinstance.The
Kirby\Http\Requestinstance will never be modified.
You can access to the Request instance transformed with:
You can add features to the global middleware in your config.php file:
[!NOTE] You can add as much middleware as requested.
They can be a
classor aClosure.
TrimStrings
TrimStrings clean all the inputs in the request, but sometimes you need to ignore some inputs to be trimmed; you can skip it with:
And you can recover those inputs with the Request instance in your controllers, models or any place required with:
Or for your convinience you can use the facade:
Group middlewares
The group middlewares will depend on routes to be triggered. By default, the group middleware comes with the web, auth and guest middleware, it brings a ValidateCsrfToken middlewares.
You can set the routes by adding the routes values in your config.php file:
[!NOTE] You can add a pattern like any
KirbyrouteBy default, the
webgroup comes with the(:all)route.The
authandguestmiddlewares are inactive by default, but you can customize the routes to enable them.
And of course, you can add more features to the web middleware in your config.php file:
If the web group is not what you need, you can add a new group of middleware. You can add it within the config.php file:
The Middleware Group should looks like:
[!IMPORTANT] All the group middleware classes should extend
Beebmx\KirbyMiddleware\MiddlewareGroups\MiddlewareGroupclass.
ValidateCsrfToken
When you use an HTML form with POST, PUT, PATCH, or DELETE in your template, you should include a hidden CSRF _token field in the form so that the CSRF protection middleware can validate the request.
[!NOTE] For convenience, you can also use
csrf,csrf-tokenor_token.
Sometimes you need to ignore some routes from the CSRF validation; you can skip it with:
Security middlewares
Middleware comes with two (auth and guest) middlewares to improve your security flow based on user authentication.
Auth middleware
The auth middleware provides a starting point to validate if the user is authenticated and if the user is able to visit given routes. If not, it will redirect to some URL to perform a proper login.
Heres an example of it:
[!NOTE] If the user is not authenticated, the middleware will redirect to a
guestpage.
Guest middleware
The guest middleware provides a starting point to validate if the visitor is a guest and is unauthenticated. If the user is authenticated, it will redirect to some URL to be inside a secured welcome page or dashboard.
Heres an example of it:
[!NOTE] If the user is authenticated, the middleware will redirect to a
authpage.
Middleware
When you create a middleware, you can use a class or a Closure; it will depend on your needs and complexity.
Middleware class
When you create your own middleware class, it should look like:
As you can see, handle requires two parameters: a Request called $request and a Closure called $next.
The $request contains the current request made in Kirby by the hook route:before.
The second parameter $next, you should call it at the end of the process to proceed to the next middleware validation with the $request.
If you need, some validations can prevent to continue with any other validation; you can throw an error or make a response redirection:
Or with an exception:
Closure middleware
The easiest way to add a global, web, auth or guest middleware is with a Closure; when you add a closure, it should look like:
[!IMPORTANT] Remember to call the
$nextclosure to proceed to the next validation with the$request.
Options
| Option | Default | Type | Description |
|---|---|---|---|
| beebmx.middleware.enabled | true | bool |
Enable/Disable all Middleware. |
| beebmx.middleware.exceptions | [] | array |
Set exceptions for trim and csrf middlewares. |
| beebmx.middleware.global | [] | array |
Add your own global middlewares. |
| beebmx.middleware.groups | [] | array |
Add your own groups middlewares. |
| beebmx.middleware.routes | [] | array |
Customize your group routes. |
| beebmx.middleware.web | [] | array |
Add your own web middlewares. |
| beebmx.middleware.auth | [] | array |
Add your own auth middlewares. |
| beebmx.middleware.guest | [] | array |
Add your own guest middlewares. |
| beebmx.middleware.redirections | [] | array |
Customize your redirections for auth and guest middlewares. |
[!WARNING] Since version
1.3.0,Middlewarechanges the plugin prefix frombeebmx.kirby-middlewaretobeebmx.middleware.Disable middleware
You can completly disable all middleware validations updating the enable value in the config.php file:
Facades
There are some facades to simplify the use of this plugin:
| Facade | Class | Instance of |
|---|---|---|
| Beebmx\KirbyMiddleware\Facades\Middleware | Beebmx\KirbyMiddleware\Middleware | Middleware::instance() |
| Beebmx\KirbyMiddleware\Facades\Pipeline | Beebmx\Pipeline\Pipeline | new Pipeline |
| Beebmx\KirbyMiddleware\Facades\Request | Beebmx\KirbyMiddleware\Request | Request::instance() |
Plugins
If you are creating your own plugin, and it's required to use some type of request manipulation, Middleware is also for you.
Installation
First, you need to inform Middleware than you have some global middleware or group middleware to register.
The easyest way to do this, is with a hook
Global methods
You can add your own validations to the global middleware. To achieve this, you have several methods.
Append
The append method adds the middleware to the end of the global middleware.
Prepend
The prepend method adds the middleware to the beginning of the global middleware.
getGlobalMiddleware
The getGlobalMiddleware method returns an array of all the global middleware registered.
Group methods
You can add your own validations to the groups middleware. To achieve this, you have several methods.
Append
The appendToGroup method adds the middleware to the end of the groups middlewares.
prependToGroup
The prependToGroup method adds the middleware to the beginning of the groups middlewares.
removeFromGroup
The removeFromGroup method removes some middleware from a specific group middleware.
addClassToGroup
The addClassToGroup method adds a Middleware Group class to the groups middlewares.
getMiddlewareGroups
The getMiddlewareGroups method returns an array of all the groups middleware registered.
Authenticate middleware
You can customize the Authenticate middleware without using options, but hook system.loadPlugins:after.
redirectUsing
To set the route to redirect if the user is not authenticated.
setRoutes
If you want to set the routes for the AuthMiddlewareGroup.
RedirectIfAuthenticated middleware
You can customize the RedirectIfAuthenticated middleware without using options, but hook system.loadPlugins:after.
redirectUsing
To set the route to redirect if the user is authenticated.
setRoutes
If you want to set the routes for the GuestMiddlewareGroup.
[!IMPORTANT] Remember, all the group middleware classes should extend
Beebmx\KirbyMiddleware\MiddlewareGroups\MiddlewareGroupclass.
Roadmap
- [ ] Custom hooks
- [ ] More
globalmiddlewares by default - [ ] More
webmiddlewares by default - [x] An
authmiddleware group. - [x] A
guestmiddleware group.
License
Licensed under the MIT.
Credits
- Fernando Gutierrez @beebmx
- All Contributors
All versions of kirby-middleware with dependencies
getkirby/composer-installer Version ^1.2
beebmx/pipeline Version ^1.0
laravel/serializable-closure Version ^1.3