I wanted to set custom headers in API so that I can call it from other platforms, it seems like the code I've written isn't working.
<?php
use Flarum\Extend;
return [
(new Extend\Middleware('api'))
->add(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
$response = $handler->handle($request);
return $response
->withHeader('Access-Control-Allow-Origin', 'https://forums.site.com https://www.site.com')
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization, Origin, Accept')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
})
];
Error log:
#0 extend.php(13): Flarum\Extend\Middleware->add(Object(Closure))
#1 vendor/flarum/core/src/Foundation/Site.php(65): require('/home/site/fo...')
#2 vendor/flarum/core/src/Foundation/Site.php(38): Flarum\Foundation\Site::loadExtenders('/home/site/fo...')
#3 site.php(47): Flarum\Foundation\Site::fromPaths(Object(Flarum\Foundation\Paths))
#4 index.php(10): require('/home/site/fo...')
#5 {main}
thrown in vendor/flarum/core/src/Extend/Middleware.php on line 39
[22-Mar-2023 03:06:33 UTC] PHP Fatal error: Uncaught TypeError: Flarum\Extend\Middleware::add(): Argument #1 ($middleware) must be of type string, Closure given, called in extend.php on line 22 and defined in vendor/flarum/core/src/Extend/Middleware.php:39
Stack trace:
#0 extend.php(22): Flarum\Extend\Middleware->add(Object(Closure))
#1 vendor/flarum/core/src/Foundation/Site.php(65): require('/home/site/fo...')
#2 vendor/flarum/core/src/Foundation/Site.php(38): Flarum\Foundation\Site::loadExtenders('/home/site/fo...')
#3 site.php(47): Flarum\Foundation\Site::fromPaths(Object(Flarum\Foundation\Paths))
#4 index.php(10): require('/home/site/fo...')
#5 {main}
thrown in vendor/flarum/core/src/Extend/Middleware.php on line 39