1
votes

Okay, so I've finally managed to get my API working in the slim framework on my MAMP localhost with this code in my index.php:

 $app = new \Slim\Slim();
 $app->get('/people', function() use ($app, $mysqli) {
     $testQuery = "SELECT name FROM People";
     $results = $mysqli->query($testQuery);

     $app->response()->header('Content-Type', 'application/json');
     while ($row = $results->fetch_assoc()) {
         echo json_encode($row);
     }
 });
 $app->run();

Now, when I go to localhost/index.php/musicians, I get exactly what I expect -- JSON encoded text representing the names on the people table. However, if I just go to localhost, I get text I printed earlier in the index.php file AND a 404 error. Moreover, when I run index.php in the console, I get this error message:

Notice: Undefined index: REQUEST_METHOD in /Applications/MAMP/htdocs/vendor/slim/slim/Slim/Environment.php on line 123

Notice: Undefined index: REMOTE_ADDR in /Applications/MAMP/htdocs/vendor/slim/slim/Slim/Environment.php on line 126

Notice: Undefined index: REQUEST_URI in /Applications/MAMP/htdocs/vendor/slim/slim/Slim/Environment.php on line 143

Notice: Undefined index: REQUEST_URI in /Applications/MAMP/htdocs/vendor/slim/slim/Slim/Environment.php on line 148

Notice: Undefined index: SERVER_NAME in /Applications/MAMP/htdocs/vendor/slim/slim/Slim/Environment.php on line 159

Notice: Undefined index: SERVER_PORT in /Applications/MAMP/htdocs/vendor/slim/slim/Slim/Environment.php on line 162

The API is clearly succeeding in creating a RESTful API, but I'm worried about what's going on with index.php and these error messages.

1
If you want localhost to return something you must define the / route to return something. And make sure you have setup your .htaccess correctly. - Odi

1 Answers

1
votes

Actually you did not provides any default value. its working because you have defined the /people but when you have provided / then slim is not getting anything what to run. So slim is showing this problem. You can solve it in the following way.Just add the following line before $app->run(); . hope your problem will be solved.

 $app->get('/', function() use ($app) {
     echo "Index";
     });