App\Middleware\Cors

CORS Middleware (Cross-Origin Resource Sharing)

This class is included with the starter site and provides a template with common options for CORS Services. Use this class with route filter callback functions so that the OPTIONS request can be handled.

This class is desinged to be easy to use without making changes and also easy to modify if you need to handle custom CORS options for your site.

Código Fonte

GitHub

Código de Exemplo

Starter Site Middleware


// The FastSitePHP Starter Site inclui várias páginas de exemplos e fornece uma
// estrutura básica de diretório / arquivo. O site foi projetado para fornecer
// estrutura para conteúdo básico (JavaScript, CSS etc.), mantendo um tamanho
// pequeno, para facilitar a remoção de arquivos desnecessários e a
// personalização para o seu site.
//
//     https://github.com/fastsitephp/starter-site
//
// As classes de Middleware são fornecidas e podem ser modificadas para
// o seu site.
//
// Para utilizá-las especifique 'Class.method' nas funções filtro da rota
// ou quando montando arquivos adicionais.

// Exige que um usuário esteja logado para utilizar uma página
$app->get('/secure-page', 'SecureController')->filter('Auth.hasAccess');

// Exige um usuário autenticado e utilize CORS
$app
    ->get('/api/:record_type', 'ApiController.getData')
    ->filter('Cors.acceptAuth')
    ->filter('Auth.hasAccess');

// Somente rode uma rota de localhost
$app->get('/server-info', function() {
    phpinfo();
})
->filter('Env.isLocalhost');

// Somente carregue um arquivo se estiver rodando à partir de localhost
$app->mount('/sysinfo/', 'routes-sysinfo.php', 'Env.isLocalhost');

Métodos

acceptAuth(Application $app)

Allow all sites to submit [Authorization] and [Content-Type] headers. This function can be used for JSON or GraphQL Services were the API or Web Service is on a different host or domain from the main site.

This function adds the following headers:
    Access-Control-Allow-Origin: {Client-Origin}
    Access-Control-Allow-Headers: Authorization, Content-Type
    Access-Control-Allow-Credentials: true

If the client does not submit an origin then the following is used:
    Access-Control-Allow-Origin: *