简体   繁体   中英

How to get the environment mode on slim framework

Picked from Slim Framework Documentation:

mode

This is an identifier for the application's current mode of operation. The mode does not affect a Slim application's internal functionality. Instead, the mode is only for you to optionally invoke your own code for a given mode with the configMode() application method.

The application mode is declared during instantiation, either as an environment variable or as an argument to the Slim application constructor. It cannot be changed afterward. The mode may be anything you want — “development”, “test”, and “production” are typical, but you are free to use anything you want (eg “foo”).

<?php
  $app = new \Slim\Slim(array(
    'mode' => 'development'
  ));
?>

The problem is, when i try to invoke $app->configMode(); I get a fatal error who says that the configMode() method is undefined...

You can access the environment mode by invoking $app->getMode();

Another way of doing it is using configureMode() .

<?php
// Set the current mode
$app = new \Slim\Slim(array(
    'mode' => 'production'
));

// Only invoked if mode is "production"
$app->configureMode('production', function () use ($app) {
    $app->config(array(
        'log.enable' => true,
        'debug' => false
    ));
});

// Only invoked if mode is "development"
$app->configureMode('development', function () use ($app) {
    $app->config(array(
        'log.enable' => false,
        'debug' => true
    ));
});

Once you configure it, you can do: $app->mode; or $_ENV["SLIM_MODE"]; to retrieve it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM