2013-05-12 53 views
1

我一直在建立一個REST API(使用菲爾鱘魚笨-Restserver)和我一直都在密切堅持教程:CodeIgniter的REST API(菲爾鱘魚) - 如何砍了一個非常大的API文件

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函數)保留下來?

任何意見或建議將是偉大的..

回答

3

您可以創建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相關的功能),以及由API使用的函數之間的分離。

我不明白你爲什麼不能使用相同的方法,只要他們返回你所需要的。

相關問題