LENKRAD - The modern PHP core
Basics
Advanced
Deployment

CORS

Dealing with cross origin requests?

The CORS class

If required, invoke an instance of the CORS class into your app.

Main methods

Method
Description

addAllowedOrigin(string $origin)

Registers an accepted origin

addAllowedMethod(string $method)

Sets an allowed method

setAllowedMethods(array $allowedMethods)

Sets allowed methods

addAllowedHeader(string $method)

Sets an allowed header

setAllowedHeaders(array $allowedHeaders)

Set allowed headers

Example


                ?php

                // /public/index.php

                use Neoan\NeoanApp;
                use Neoan\Routing\Route;
                use Neoan\Helper\Setup;
                use Neoan\Cors\Cors;

                ...

                // 1. setup cors
                $cors = new Cors();
                $cors->addAllowedOrigin('*')
                     ->setAllowedMethods(['GET','OPTIONS', 'POST', 'PUT'])
                     ->setAllowedHeaders(['Content-Type', 'X-Auth-Token', 'Authorization', 'Origin'])

                $app = new NeoanApp($setup);

                // 2. invoke
                $app->invoke($cors);

                // 3. make instance available for Routables (optional)
                $app->injectionProvider->set(Cors::class, $cors);

                ...

                // run application
                $app->run();
            

Before you move on

Many references on this page assume default settings. Your project might differ in behavior, paths etc.