我试图访问slim php 3.1中的设置
的src / settings.php文件
<?php
return [
'settings' => [
'displayErrorDetails' => true, // set to false in production
'addContentLengthHeader' => false, // Allow the web server to send the content-length header
// Renderer settings
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
],
// Monolog settings
'logger' => [
'name' => 'slim-app',
'path' => __DIR__ . '/../logs/app.log',
'level' => \Monolog\Logger::DEBUG,
],
],
];
检索设置
由于设置存储在DI容器中,因此您可以访问它们 通过容器工厂中的设置键。例如:
$ settings = $ container-&gt; get(&#39;设置&#39;)[&#39;记录器&#39;];
您还可以通过$ this:
在路线中调用来访问它们$ app-&gt; get(&#39; /&#39;,function($ request,$ response,$ args){
$loggerSettings = $this->get('settings')['logger']; // ...
});
公开/ index.php的
require '../vendor/autoload.php';
$app = new \Slim\App;
$app->get('/profile/facebook/{id}', function (Request $request, Response $response) use ($app){
var_dump($this->get('settings')['logger']);
});
$app->run();
我得到了NULL。
我做错了什么?
答案 0 :(得分:2)
您需要将设置传递到App:
require '../vendor/autoload.php';
use Slim\Http\Request;
use Slim\Http\Response;
$settings = include __DIR_ . '/../src/settings.php';
$app = new \Slim\App($settings);
$app->get('/profile/facebook/{id}', function (Request $request, Response $response) {
var_dump($this->get('settings')['logger']);
});
$app->run();