2016-11-30 55 views
0

如何在Slim中寫入數組輸出?Slim Framework - 如何輸出沒有模板的數組?

$app->get('/', function ($request, $response, $args) { 
    $array = ['message' => 'Hello World']; 
    $response->getBody()->write($array); 
    return $response; 
}); 

錯誤:

Slim Application Error The application could not run because of the following error:

Details

Type: RuntimeException Message: Could not write to stream File: /var/www/slim/vendor/slim/slim/Slim/Http/Stream.php Line: 407

我只想陣列輸出到屏幕上沒有任何模板,也沒有JSON。可能嗎?

回答

2

因爲你不能echo數組,你需要形成一個字符串,然後可以寫入輸出流。

$response->getBody()->write(print_r($array, true)); 

print_r的第二個參數是它應該返回值而不是直接打印它。

PHPDoc指定寫入方法只接受字符串,參見\Slim\Http\Response