Pckg Documentation
Pckg Documentation
  • About
  • Concept
  • Start
  • Deployment
  • Tests
  • Ecosystem
    • Docker images
    • Skeleton
  • Core packages
    • Auth
    • Cache
    • Collection
    • Concept
    • Database
      • Repository
        • Driver
      • Entity
        • Relations
        • Query
      • Record
        • Fields
        • Model
      • Events
      • Extensions
    • Framework
      • Environment
        • Console
        • Development
        • Production
        • Test
      • Application
      • Providers
      • Config
      • Router
      • Request
      • Response
      • Controller
        • Middleware
        • Afterware
      • View
      • Events
      • Exception
    • Generic
      • Dynamic
      • Generic
      • Maestro
    • Htmlbuilder
      • Elements
        • Form
        • Fields
      • Datasources
      • Validators
      • Decorators
    • Locale
    • Mail
      • Drivers
      • Template
      • Mail
    • Manager
      • Asset manager
      • SEO manager
      • Meta manager
      • Upload manager
      • Vue manager
      • Job manager
      • Locale manager
      • Page manager
    • Migrator
      • Migrations
      • Fields
    • Queue
      • Driver
      • Publisher
      • Subscriber
    • Storage
      • Driver
      • Media
    • Translator
  • More packages
    • API
    • HTTP QL
      • Read
      • Write
      • Uploads
    • Task
      • Async
    • Websocket
      • Server
      • Client
  • Frontend
    • Helpers JS
    • Helpers CSS
  • Extras
    • Parser
    • Payment
    • Tenant
Powered by GitBook
On this page

Was this helpful?

  1. Core packages
  2. Framework
  3. Controller

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.

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

        return $next();
    }
}

They can be defined on application / provider level:

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

... or route level:


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

Last updated 2 years ago

Was this helpful?