2017-01-11 6 views
-2

cakephpを使ってRESTAPIを作成したいと思います。私はcakephpの新人です。そして、私はちょうど2日前にそれについてのみ仕事を始めました。私がRESTAPIを作成して使用するのを助けてください。cakephpでRESTAPIを作成する方法

私はcakephp3の最新バージョンを使用しています。私のシステムではubuntuソフトウェアを使用しています。

<?php 
namespace App\Controller; 

use App\Controller\AppController; 

use Cake\Cache\Cache; 

class EmployeesController extends AppController 
{ 

    public function initialize() 
    { 
     parent::initialize(); 
     $this->loadComponent('RequestHandler'); 
    } 
    public function index() 
    { 
     // find('all') get all records from Topics model 
     // We uses set() to pass data to view 
     $employees = $this->Employees->find('all'); 

     $this->set([ 
        'employees'=>$employees, 
        '_serialize'=>['employees'] 
        ]); 
    } 


    public function view($id) 
    { 

     // get() method get only one topic record using 

     // the $id paraameter is received from the requested url 

     // if request is /topics/view/5 the $id parameter value is 5 

     // if request is /topics/view/5.json the $id parameter value is 
     $employee = $this->Employees->get($id); 

     $this->set([ 
      'employee'=>$employee, 
      '_serialize'=>['employee'] 
      ]); 
    } 

    public function add() 
    { 
     $employee = $this->Employees->newEntity(); 

     //if the user topics data to your application, the POST request informations are registered in $this->request 

     if ($this->request->is('post')) { // 
      $employee = $this->Employees->patchEntity($employee, $this->request->data); 
      $employee->employee_id = $this->Auth->user('id'); 

      if ($this->Employees->save($employee)) { 
       $message = 'employee saved'; 
      } else { 
       $message = 'Error1'; 
      } 
     } 

     $this->set([ 
      'message' => $message, 
      'employee' => $employee, 
      '_serialize' => ['message', 'employee'] 

     ]); 
    } 

    public function edit($id = null) 
    { 
     $employee = $this->Employees->get($id); 
     if ($this->requeEmployees 
      $this->Employees->patchEntity($employee, $this->request->data); 

      if ($this->Employees->save($employee)) { 
       $message = "Employee updated"; 
      } else { 
       $message = "Error2"; 
      } 
     } 

     $this->set([ 
       'message'=>$message, 
       'employee'=>$employee, 
       '_serialize'=>['message','employee'] 
       ]); 
    } 

    public function delete($id) 
    { 
     $this->request->allowMethod(['post', 'delete']); 

     $employee = $this->Employees->get($id); 
     if ($this->Employees->delete($employee)) {  
      $message = 'Employee deleted'; 
     } else { 
      $message = 'Error3'; 
     } 

     $this->set([ 
      'message'=>$message, 
      '_serialize'=>['message'] 
     ]); 
    } 
} 
?> 
+0

マニュアルの読み方はどうですか? https://book.cakephp.org/3.0/ja/index.html RESTに関する章が含まれています。 – burzum

答えて

0

あなたは間違いなくhttps://book.cakephp.org/3.0/en/index.htmlにケーキのドキュメントと「料理」を介して移動する必要があり、それはあまりにも難しいことではありません。そして、これは疑問には思われませんが、実際にcakephpでコードを書くように教える人を尋ねるのです。

関連する問題