App\Middleware\Env

Environment Middleware

This class is included with the starter site and provides a template with common options for environment middleware. Modify this class as needed for your site.

Example usage that only allows a route if the user is on a local network and then loads a [.env] file prior to the route running:

    $app->get('/url', 'Controller')
        ->filter('Env.isLocalNetwork')
        ->filter('Env.loadDotEnv');

Source Code

GitHub

Example Code

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');

Methods

isLocalhost()

Return true if the request is running from localhost '127.0.0.1' (IPv4) or '::1' (IPv6) and if the web server is also running on localhost.

Returns: bool

isLocalNetwork()

Return true if the web request is coming a local network. (for example 127.0.0.1 or 10.0.0.1).

Returns: bool

isLocalFromProxy()

Return true if the web request is coming a local network and and a Proxy Server such as a Load Balancer is being used.

Returns: bool

loadDotEnv(Application $app)

Loads environment variables from a [.env] file into [getenv()] and [$_ENV].