php - 如何在 slim 框架上获取环境模式

标签 php slim

摘自 Slim 框架文档:

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 (e.g. “foo”).

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

问题是,当我尝试调用 $app->configMode(); 时出现 fatal error ,指出 configMode() 方法未定义...

最佳答案

您可以通过调用 $app->getMode();

访问环境模式

另一种方法是使用 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
    ));
});

关于php - 如何在 slim 框架上获取环境模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15948250/

相关文章:

php - 状态 200 = HttpPut 有效吗?

php - Windows Composer 安装问题

php - 如何在 Codeigniter 中两次连接同一个表并分配表别名?

javascript - ajax 响应的访问元素以数组字符串的形式出现

php - PHP Slim 3 中间件的不合理错误

php - 突出显示字符串中的多个单词

php - MYSQL:根据当前时间返回列时间值?

javascript - 无法使用 Slim REST 在 AngularJS 上添加新输入

php - 如何使用 Slim Framework 转发 HTTP 请求