使用 Slim Framework 在另一个 api 中调用内部 api [英] Calling an internal api within another api in using Slim Framework

查看:23
本文介绍了使用 Slim Framework 在另一个 api 中调用内部 api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

我正在尝试使用 Slim 框架开发 Web 平台.我已经以 MVC 的方式完成了.我的一些 API 用于呈现视图,而另一些只是为了从数据库中获取数据而构建的.例如:

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
    ));
});

我正在尝试在视图相关的 api '/adminpage' 中调用数据库处理 Api '/api/getListAdmin'.

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

根据我在网上找到的解决方案,我尝试了方法 1 和 2,但是:

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

  • 方法 1(使用 file_get_contents)需要很长时间才能获取数据(在我的本地环境中几秒钟).

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

方法 2 (router->getNamedRoute->dispatch) 似乎不起作用,因为即使我使用 $result = $route->dispatch(); 它也会在视图中呈现结果;将结果存储在一个变量中,但似乎调度方法仍然将结果呈现到屏幕上.

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.

我尝试仅为与数据库相关的 API 创建一个新的超薄应用程序,但仍然调用其中一个需要相当长的时间 2 到 3 秒.

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.

如果有人可以帮助我解决我做错了什么或者从另一个 api 获取数据的正确方法是什么,我真的很感激.

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.

谢谢

推荐答案

方法一

这可能是另一种方法,创建一个Service层,删除冗余代码:

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
    ));
});

方法二

如果你对一个矫枉过正的方法没问题,你可以使用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
  ));
});

这篇关于使用 Slim Framework 在另一个 api 中调用内部 api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆