簡體   English   中英

使用CakePHP創建REST API的參數

[英]What parameters to create REST API using CakePHP

我正在創建用於使用CakePHP登錄的REST API。 我的問題是:

  1. routes.php ,我該如何填寫mapresources("xxx")
  2. POST /XXX.format XXXController::add() <=這在文檔中給出。 如果我的應用程序文件夾是這樣的: /localhost/FC/app/webroot/等。發布請求的URL是什么,我將通過該URL發送JSON格式的用戶名和密碼? 目前,我通過鍵入localhost/FC訪問webroot中的index.php
  3. 如果我將控制器命名為Apis而不是下面的配方,例如ApisController.php ,那么我在下面的代碼中應該做哪些更改? 以及如何使用添加? 它沒有在文檔中給出:

     class RecipesController extends AppController { public $components = array('RequestHandler'); public function index() { $recipes = $this->Recipe->find('all'); $this->set(array( 'recipes' => $recipes, '_serialize' => array('recipes') )); } public function view($id) { $recipe = $this->Recipe->findById($id); $this->set(array( 'recipe' => $recipe, '_serialize' => array('recipe') )); } public function edit($id) { $this->Recipe->id = $id; if ($this->Recipe->save($this->request->data)) { $message = 'Saved'; } else { $message = 'Error'; } $this->set(array( 'message' => $message, '_serialize' => array('message') )); } public function delete($id) { if ($this->Recipe->delete($id)) { $message = 'Deleted'; } else { $message = 'Error'; } $this->set(array( 'message' => $message, '_serialize' => array('message') )); } } 
  4. 最后,如果我將json中的用戶ID密碼發送到此網址,我該怎么做才能返回200 OK響應?

我對它有點了解,但是我確實是一個新手,即使我已經使用了3天並且將要精疲力盡,但我還是無法掌握這個概念。 請幫忙!

現在,控制器是客戶:

public function login() {
           if ($this->Session->check('Customer')) {  //to check if already logged in



            $this->Session->setFlash('You are already logged in as ' . $this->Session->read('Customer.Customer.fname') . ' ' . $this->Session->read('Customer.Customer.sname'));
            $this->redirect($this->Session->read('ref'));
        } else {
            if ($this->request->is('post')||$this->request->is('ajax')) {   //receives data by ajax from popup of login


                $name = $this->request->data('name');
                $pwd = $this->request->data('pwd');
                $pwd = md5($pwd);   //hashing of password
                $customer = $this->Customer->findByEmail($name);
                if (!$customer) {
                    $msg = 'Wrong Username or password/false';
                }   
                if ($customer['Customer']['active'] == 1) {


                    $customer = $this->Customer->findByEmailAndPassword($name, $pwd);

                    if (@$customer) {
                        $this->Session->write('Customer', $customer);
                      $msg = $customer['Customer']['fname'].'/true';

                        if ($this->Session->check('order')) {
                            $msg = $this->Session->read('loc_id').'/set';

                        } 
                    } else {
                        $msg = 'Wrong Username or password/false';
                    }
                } else {
                    $msg = 'Your account in not active. Please check your mails to get the activation link/false';
                }


            }
        }
        echo $msg;
  1. 如果使用名為ApisController的控制器,則必須用api填充mapresource。 示例: Router::mapResources('api');

  2. 這些是創建的默認路由:

    • GET /apis.format RecipesController :: index()
    • GET /apis/123.format RecipesController :: view(123)
    • POST /apis.format RecipesController :: add()
    • PUT /apis/123.format RecipesController :: edit(123)
    • DELETE / apis / 123.format RecipesController :: delete(123)
    • POST /apis/123.format RecipesController :: edit(123)

因此,如果您的主頁位於: http://localhost/FC/ ,則可以訪問http://localhost/FC/apis.format的資源。

您必須用json或xml替換格式。 如果你想使用XML或JSON你必須聲明它在routes.php添加Router::parseExtensions();

  1. 您必須在ApisController中重命名控制器並更改$ this-> Api中$ this-> Recipe的每一次出現,還必須為Api創建一個Model並在數據庫上創建一個表。 對於xml和json,您必須在/app/Views/Apis/xml/index.ctp等中創建視圖。

    // app / View / Apis / xml / index.ctp //對$ recipes數組進行一些格式化和操作。 $ xml = Xml :: fromArray(array('response'=> $ apis)); echo $ xml-> asXML();

最后一個答案,如果沒有錯誤,則服務器通常以200回答。

我建議您從簡單一些開始,並着眼於Cakephp約定。 http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html

干得好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM