codeigniter:REST API +的Json [英] codeigniter: Rest api + Json

查看:272
本文介绍了codeigniter:REST API +的Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何,我实现code点火器的REST API,使用Ajax输出JSON输出。我不能确定如何使用REST API删除,并与JSON增加。如何从JSON从数据库中删除一个项目,然后。

How, do I implement code igniter rest API that outputs with ajax outputting the JSON. I'm unsure how to use the REST API for deleting and adding with the JSON. How to delete a item from the JSON and then from the database.

推荐答案

1。 REST API路由

让我们先从你的API的路由 - 如果你申请最佳实践你的API,你会希望使用HTTP动词(POST / PUT / GET ...)。 (了解更多关于HTTP动词和REST这里

Let's start with the routing of your API - If you apply best practice to your API you will want to use HTTP verbs (POST/PUT/GET...). (Read more on HTTP Verbs and REST here)

如果您是在codeigniter 2.x的话<一个href=\"http://stackoverflow.com/questions/22552071/$c$cigniter-2-1-4-support-for-http-methods-custom-routings\">read这太问题,并回答

If you are on Codeigniter 2.x then read this SO question and answer

如果您是在codeigniter 3.x的话的阅读本文档

If you are on Codeigniter 3.x then read this documentation

在另一边,如果你打算使用API​​仅在内部和项目的范围是有限的,可以忽略HTTP动词,只是让POST和GET调用你的控制器。

On the other side, if you are planning to use the API only internally and the scope of the project is limited you can just ignore HTTP verbs and simply make POST and GET calls to your controllers.

2。创建JSON响应

一般来说,你将不再需要使用视图 - 你可以有你的控制器回声JSON响应。下面是有一个API调用来从演员的数据库中获取一个演员一个虚构的API的例子。

Generally you will not need to use views - you can have your controllers echo the JSON response. Here is an example of a fictional API that has an API call to get an actor from a database of actors.

/*   ----------------------------------------------
|    Example of an API Method to get an actor from
|    a database of actors:
|    call: http://example.com/api/GetActorById
|    parameter: actor_id
|    Method: POST
*/
public function GetActorById(){

    // Let's first get the actor ID from the POST data
    $actor_id = $this->input->post('actor_id');

    // If the API Method would be GET you would get the
    // actor ID over the URI - The API call would look
    // like this: http://example.com/api/GetActorById/<actor_id>

    // In that case you take your actor ID from the segment:
    $actor_id = $this->uri->segment(3);


    // Do your database query magic here!
    // ...
    // ...

    // Let's create an array with some data 
    // In real life this usually comes from a DB query above
    $data = array(
            'Firstname' => 'Michael',
            'Lastname' => 'Fox',
            'IsActor' => True,
            'UserId' => 1234567
            );

    // Now let's convert the array into a JSON
    $json = json_encode($data);

    // if the API is accessed from a different domain
    // you will want to allow cross domain access
    header('Access-Control-Allow-Origin: *'); 

    // Now let's return the json
    echo $json;
}

输出将是这样的:

The output will look like this:

{"Firstname":"Michael","Lastname":"Fox","IsActor":true,"ActorID":123456789}

希望这有助于 - 祝你好运与您的项目

Hope this helps - Good luck with your project!

这篇关于codeigniter:REST API +的Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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