> For the complete documentation index, see [llms.txt](https://pckg.gitbook.io/documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pckg.gitbook.io/documentation/core-packages/index-5/index-7/middleware.md).

# Middleware

Middlewares are nothing else than simple filters or commands that get executed before controller action gets called. You can use them for restricting access, check for autologin cookie, log requests or in any other way. All you have to do is implement `execute(callable $next)` method and return `$next()` if you want to continue with execution.

With bottom example we restrict remote access and allow only access from localhost.

```php
class RestrictAccess {
    public function execute() {
        if (server('REMOTE_ADDR') !== '127.0.0.1') {
            return;
        }

        return $next();
    }
}
```

They can be defined on application / provider level:

```php
public function middlewares() {
    return [
        RestrictAccess::class,
    ];
}
```

... or route level:

```php

public function routes() {
    return [
        route('/route', 'action', Controller::class)
            ->middlewares([
                RestrictAccess::class,
            ]),
    ];
}
```
