通过使用纯CodeIgniter创建RESTful API? [英] Creating RESTful API by using pure CodeIgniter?

查看:210
本文介绍了通过使用纯CodeIgniter创建RESTful API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要使用CodeIgniter创建一个RESTful Web api。我不能使用任何第三方插件或库来执行此操作。我已经看到大多数人都在使用 https://github.com/chriskacerguis/codeigniter-restserver。请指导我仅使用CodeIgniter编写REST api。有用的链接和步骤受到高度赞赏。

I need to create a RESTful web api using only CodeIgniter. I can not use any third-party plugins or libraries to do this. I have seen that most people are using https://github.com/chriskacerguis/codeigniter-restserver. Please guide me on writing a REST api only using CodeIgniter. Helpful links and steps are highly appreciated.

谢谢。

推荐答案

如果您使用的是版本3,则可以执行此操作

If your are using version 3, you can do this

创建控制器users.php

create a controller users.php

class Users extends CI_Controller {

    /**
    * @route http://proyect/users
    * @verb GET
    */
    public function get()
    {
        echo "Get";
    }

    /**
    * @route http://proyect/users
    * @verb POST
    */
    public function store()
    {
        echo "Add";
    }

    /**
    * @route http://proyect/users
    * @verb PUT
    */
    public function update()
    {
        echo "Update";
    }

    /**
    * @route http://proyect/users
    * @verb DELETE
    */
    public function delete()
    {
        echo "Delete";
    }

}

在应用程序中进行编辑(添加) /config/route.php

edit (add) in your application/config/route.php

$route["users"]["get"]    = "users/get";
$route["users"]["post"]   = "users/store";
$route["users"]["update"] = "users/update";
$route["users"]["delete"] = "users/delete";

$route['products/([a-zA-Z]+)/edit/(\d+)'] = function ($product_type, $id)
{
        return 'catalog/product_edit/' . strtolower($product_type) . '/' . $id;
};

这篇关于通过使用纯CodeIgniter创建RESTful API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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