班级清单
- App\Middleware\Cors
- App\Middleware\Auth
- App\Middleware\Env
- AppMin
- Application
- Route
- Data\Database
- Data\Db2Database
- Data\OdbcDatabase
- Data\Validator
- Data\KeyValue\SqliteStorage
- Data\Log\FileLogger
- Data\Log\HtmlLogger
- Encoding\Base64Url
- Encoding\Json
- Encoding\Utf8
- Environment\DotEnv
- Environment\System
- FileSystem\Search
- FileSystem\Security
- FileSystem\Sync
- Lang\I18N
- Lang\L10N
- Lang\Time
- Media\Image
- Net\Config
- Net\Email
- Net\HttpClient
- Net\HttpResponse
- Net\IP
- Net\SmtpClient
- Security\Crypto
- Security\Password
- Security\Crypto\Encryption
- Security\Crypto\FileEncryption
- Security\Crypto\JWT
- Security\Crypto\PublicKey
- Security\Crypto\Random
- Security\Crypto\SignedData
- Security\Web\CsrfSession
- Security\Web\CsrfStateless
- Security\Web\RateLimit
- Web\Request
- Web\Response
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 designed to be easy to use without making changes and also easy to modify if you need to handle custom CORS options for your site.
源代码
相关链接
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
- http://www.html5rocks.com/en/tutorials/cors/
- http://www.w3.org/TR/cors/
- http://www.w3.org/TR/cors/#access-control-allow-origin-response-header
- https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
示例代码
Starter Site Middleware
// The FastSitePHP Starter Site includes several examples pages and provides
// a basic directory/file structure. The site is designed to provide structure
// for basic content (JavaScript, CSS, etc) while remaining small in size so
// that it is easy to remove files you don’t need and customize it for your site.
//
// https://github.com/fastsitephp/starter-site
//
// Core Middleware classes are provided and can be modified for your site.
//
// To use them specify the 'Class.method' on route filter functions or
// when mounting additional files.
// Require a user to be logged in in order to use a page
$app->get('/secure-page', 'SecureController')->filter('Auth.hasAccess');
// Require an authenticated user and use CORS
$app
->get('/api/:record_type', 'ApiController.getData')
->filter('Cors.acceptAuth')
->filter('Auth.hasAccess');
// Only run a route from localhost
$app->get('/server-info', function() {
phpinfo();
})
->filter('Env.isLocalhost');
// Only load a file if running from localhost
$app->mount('/sysinfo/', 'routes-sysinfo.php', 'Env.isLocalhost');
方法
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: *