0% found this document useful (0 votes)
36 views11 pages

Creating Basic Crud With Rest

This document discusses creating basic CRUD (create, read, update, delete) functionality using REST (Representational State Transfer). REST uses HTTP request methods like GET, POST, PUT, and DELETE to perform CRUD operations. GET retrieves data, POST creates data, PUT updates data, and DELETE deletes data. The document then provides an example of implementing CRUD for a news application in CakePHP using REST requests mapped to controller actions. It shows how to get a list of news items, create new entries, update existing entries, and delete entries.

Uploaded by

Vishnu N Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views11 pages

Creating Basic Crud With Rest

This document discusses creating basic CRUD (create, read, update, delete) functionality using REST (Representational State Transfer). REST uses HTTP request methods like GET, POST, PUT, and DELETE to perform CRUD operations. GET retrieves data, POST creates data, PUT updates data, and DELETE deletes data. The document then provides an example of implementing CRUD for a news application in CakePHP using REST requests mapped to controller actions. It shows how to get a list of news items, create new entries, update existing entries, and delete entries.

Uploaded by

Vishnu N Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CREATING BASIC CRUD

WITH REST

What is CRUD?
CRUD-

CREATE
READ
UPDATE
DELETE

REST
REST stands for Representational State Transfer
Any application conforming to this standard is called
RESTful.
REST is very simple and based on HTTP request
methods:
GET, POST, DELETE, and PUT.
The last two methods are not so popular in
common application development, but PUT and DELETE
can be used in the same way as POST and GET.

REST is based on all four methods to


implement a create, read, update, and delete (CRUD)
application without filling any forms, but just by simple
method-invoking.
Comparing REST to CRUD operations,
GET would be read, POST would be create, DELETE
would
be delete, and PUT would be update.

What is cURL?
cURL is tool that allows for data between miscellaneous
protocols, for example http, https, ftp, telnet.
You can grab it at http://curl.haxx.se/
LibcURL is a library used also by PHP.
It's installed with XAMPP under Windows and should
also be available out of the box when installing PHP
under UNIX systems.
What you need to do is to make sure that cURL is
enabled in php.ini.
In Windows, there should be an entry like the following:

CakePHP supports REST.


To make it possible to access CakePHP modules through
REST requests, you may also invoke a resource mapper
method that maps REST methods (GET, POST,
PUT,DELETE) to CakePHP's controller CRUD equivalents
index(), add(), edit(), and delete(). Both sets of methods
should be added in the app/config/
routes.php configuration file:
Router::mapResources(news);
Router::parseExtensions();

Getting a List of News


Let's assume that a news controller is placed in
/controllers/news_controller.php as shown in the
following code for index() and view() actions:
<?php
class NewsController extends AppController {
var $components = array(RequestHandler);
function index() {
$recipes = $this->News->find(all);
$this->set(compact(news));
}

function view($id) {
$recipe = $this->News->findById($id);
$this->set(compact(news));
}
}

Creating New Entries


Creating new entries is done through sending POST
data.
function add() {
$this->layoutPath=xml;
if ($this->News->save($this->data)) {
$message = Saved successfully;
} else {
$message = Error while saving item;
}
$this->set(message,$message);
$this->render(message);
}

Updating News

Editing entries should be done almost in the same way as


creating new items:
function edit($id) {
$this->layoutPath=xml;
$this->News->id = $id;
if ($this->News->save($this->data)) {
$message = News updated successfully;
} else {
$message = Error while updating item;
}
$this->set(message,$message);
$this->render(message);
}

Deleting News
Deleting news can be done very easily by using the news
ID as a parameter to the model's delete() method:
function delete($id) {
if($this->News->delete($id)) {
$message = News deleted successfully;
} else {
$message = Error while deleting item;
}
$this->set(message,$message);
$this->render(message);
}

You might also like