How in Slim framework to read the contents of a file and display it? For example, the code below works in a regular PHP application:

<?php $data= json_decode(file_get_contents('C:\Users\Admin\Desktop\s1\vendor\me.json'), true); print data["data"][0]["id"]; 

But, if I write the following code in Slim, then nothing comes out:

 use Slim\Http\Request; use Slim\Http\Response; require 'vendor/autoload.php'; $app->get('/test/', function(Request $reg, Response $res, $args = []){ $data= json_decode(file_get_contents('C:\Users\Admin\Desktop\s1\vendor\me.json'), true); return $res->$data["data"][0]["id"]; }); 

Tell me what am I doing wrong?

    1 answer 1

    If you want to display only, $data["data"][0]["id"] , then try

     $res->getBody()->write($data["data"][0]["id"]); return $res;