调用另一个API中的内部API中使用超薄框架 [英] Calling an internal api within another api in using Slim Framework

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

问题描述

您好,

我试着去开发使用超薄框架的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的方法(路由器 - > getNamedRoute->调度),似乎dosnt工作becuz它会使结果,即使我用$结果= $为route->调度()的观点;将结果存储在一个变量,但似乎调度方法仍然呈现导致在屏幕上。

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.

我试图创建仅适用于DB相关的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.

真的AP preciate,如果有人可以帮助我什么,我做错了或什么是从另一个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.

感谢

推荐答案

这可能是另一种方法,创建一个服务层,其中冗余code被删除:

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

方法2

如果你都OK带有矫枉过正的方法,你可以使用 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
  ));
});

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

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