doutao4938 2013-03-03 16:40
浏览 48

带有Slim Framework的MySQL API

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条回答 默认 最新

  • dpd20130 2014-12-26 23:15
    关注

    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";
         });
    
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)