我需要仅使用 CodeIgniter 创建 RESTful Web api。我无法使用任何第三方插件或库来执行此操作。我看到大多数人都在使用https://github.com/chriskacerguis/codeigniter-restserver https://github.com/chriskacerguis/codeigniter-restserver。请指导我仅使用 CodeIgniter 编写 REST api。非常感谢有用的链接和步骤。
提前致谢。
如果您使用的是版本 3,您可以这样做
创建一个控制器 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";
}
}
在您的 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;
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)