sw-fw-less
  • sw-fw-less
  • get started
    • Installation
    • Directory
    • Config
    • Quick Start
  • MVC
    • Router
    • Middleware
    • Request
    • Param Validator
    • Model
    • Response
  • Database
    • MySQL
    • Redis
    • Elasticsearch
  • Storage
    • General
    • File
    • Qiniu
    • Alioss
  • OTHER
    • Pagination
    • Helper
    • Error Handler
    • AMQP-0-9-1
    • Events
  • Deployment
    • Nginx
    • Docker
  • APPENDIX
    • Development Rules
Powered by GitBook
On this page
  1. MVC

Middleware

PreviousRouterNextRequest

Last updated 6 years ago

Was this helpful?

CtrlK

Was this helpful?

It supports multiple middlewares. You can set both route middlewares and global middlewares. The priority of router middleware is lower than the one of global middleware.

Example

<?php

namespace App\middlewares;

use App\components\Config;
use App\components\Request;

class Cors extends AbstractMiddleware
{
    /**
     * @param Request $request
     * @return \App\components\Response|mixed
     */
    public function handle(Request $request)
    {
        if (Config::get('cors.switch')) {
            return $this->next($request)->header('Access-Control-Allow-Origin', (string)Config::get('cors.origin'));
        }

        return $this->next($request);
    }
}