我一直在构建一个rest api (使用Phil Sturgeons Codeigniter-Restserver),并且一直在紧跟教程:
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/特别是,我一直在关注本教程的这一部分:
function user_get()
{
// respond with information about a user
}
function user_put()
{
// create a new user and respond with a status/errors
}
function user_post()
{
// update an existing user and respond with a status/errors
}
function user_delete()
{
// delete a user and respond with a status/errors
}我已经为api可访问的每个数据库对象编写了上述函数,而且:
function users_get() // <-- Note the "S" at the end of "user"
{
// respond with information about all users
} 我目前有大约30个数据库对象(用户、产品、客户端、事务等),所有这些对象都有为它们编写的上述函数,并且所有函数都被转储到/controllers/api/api.php中,这个文件现在已经变得非常大(超过2000行代码)。
问题1:
有没有办法将这个api文件拆分成30个文件,并将与单个数据库对象相关的所有api函数保存在单个位置,而不是将所有api函数都转储到单个文件中?
问题2:
我还希望将我当前的模型函数(与api无关的函数)与api使用的函数分开。
我应该这么做吗?有没有我应该在这里使用的推荐方法?例如,我是否应该编写api使用的单独模型,或者是否可以将给定数据库对象的所有模型函数(包括非api函数和api函数)保存在同一文件中?
任何反馈或建议都是很棒的..
发布于 2013-05-13 03:01:16
您可以像创建常规控制器一样创建api控制器;您可以对模型执行相同的操作。
application/controllers/api/users.php
class Users extends REST_Controller{
function user_post(){
$this->users_model->new_user()
...
POST index.php/api/user--
application/controllers/api/transactions.php
class Transactions extends REST_Controller{
function transaction_get(){
$this->transactions_model->get()
...
GET index.php/api/transaction我还希望将我当前的模型函数(与
无关的函数)与api使用的函数分开。
我不明白为什么您不能使用相同的方法,只要它们返回您需要的内容。
https://stackoverflow.com/questions/16510320
复制相似问题