duanjiashai9140 2015-06-22 02:56 采纳率: 0%
浏览 53
已采纳

使用Slim Framework在另一个api中调用内部api

Good day,

Im trying to develop a web platform using Slim framework. I've done it in MVC way. some of my APIs are used to render the view and some is just built to get data from the db. for example :

$app->get('/api/getListAdmin', function () use ($app) {
    $data = ...//code to get admins' list
    echo json_encode($data);
})->name("getListAdmin");





$app->get('/adminpage', function () use ($app) {

    // **** METHOD 1 :// get the data using file_get_contents
    $result = file_get_contents(APP_ROOT.'api/getListAdmin');

    // or 

    // **** METHOD 2 :// get data using router
    $route = $this->app->router->getNamedRoute('getListAdmin');
    $result = $route->dispatch();
    $result = json_decode($result);        

    $app->render('adminpage.php',  array(
        'data' => $result
    ));
});

I'm trying to call the db handling Api '/api/getListAdmin' within the view related apis '/adminpage'.

based on solutions i have found in the web i tried method 1 and 2 but:

  • method 1 (using file_get_contents) take a long time to get the data (few seconds on my local environment).

  • method 2 (router->getNamedRoute->dispatch) seems dosnt work becuz it will render the result in the view even if i use $result = $route->dispatch(); to store the result in a variable but seems dispatch method still render to result to the screen.

I tried to create a new slim app only for db related API but still calling one of them takes quite long time 2 to 3 seconds.

Really appreciate it if some one can help me on what i'm doing wrong or what is the right way to get data from another api.

Thanks

  • 写回答

1条回答 默认 最新

  • douju9847 2015-06-22 12:16
    关注

    Method 1

    This could be another method, creating a Service layer, where redundant code is deleted:

    class Api {
        function getListAdmin() {
            $admins = array("admin1", "admin2", "admin3"); //Retrieve your magic data
            return $admins;
        }
    }
    
    $app->get('/api/getListAdmin', function () use ($app) {
        $api = new Api();
        $admins = $api->getListAdmin();
        echo json_encode($admins);
    })->name("getListAdmin");
    
    
    $app->get('/adminpage', function () use ($app) {
        $api = new Api();
        $admins = $api->getListAdmin();      
        $app->render('adminpage.php',  array(
          'data' => $admins
        ));
    });
    

    Method 2

    If you are ok with an overkill method, you could use Httpful:

    $app->get('/adminpage', function () use ($app) {
      $result = \Httpful\Request::get(APP_ROOT.'api/getListAdmin')->send();
    
      //No need to decode if there is the JSON Content-Type in the response
      $result = json_decode($result);
      $app->render('adminpage.php',  array(
        'data' => $result
      ));
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料