public
Created

Deleting with REST, SLIM and JSON

  • Download Gist
Name.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
<?php
session_start();
if (!isset($_SESSION['names']))
$_SESSION['names'] = array("jane","jim","john","emily","bill","sara");
 
 
 
class Name
{
public $id;
public $name;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
public function create()
{
$_SESSION['names'][] = $this->name;
}
public function update()
{
$_SESSION['names'][$this->id] = $this->name;
}
 
public static function delete($id)
{
unset($_SESSION['names'][$id]);
}
public static function findAll()
{
$names = array();
foreach($_SESSION['names'] as $id => $name)
$names[] = new Name($id , $name);
return $names;
}
public static function find($id)
{
return new Name($id, $_SESSION['names'][$id]);
}
 
 
 
}
home.tpl.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
<html>
<head>
<title>Names Hello</title>
<script type="text/javascript" src="/js/jquery-1.5.1.js"></script>
<style>
*
{
 
margin: 0 0;
padding: 0 0;
font-family: helvetica, arial, sans-serif;
}
body {
 
background: #CCC;
}
#container
{
padding: 20px;
margin: 0 auto;
width: 600px;
background: #FFF;
}
 
#names
{
width:150px;
}
.delete
{
margin-top:4px;
font-size: 9px;
float: right;
}
 
</style>
<script>
$(function()
{
$("#names").delegate('.edit','click',function(e)
{
$(this).hide()
$(this).parent().append('<input type="text" value="'+$(this).text()+'"></input><button class="save">save</button>');
});
$("#names").delegate('a.delete','click',function(e)
{
$.ajax({type: 'DELETE',
dataType: 'json',
url: '/names/'+$(this).parents('li').data('id'),
success: function(resp)
{
renderNames(resp);
}});
e.preventDefault();
});
$("#names").delegate('button.save','click',function(e)
{
$.ajax({type: 'PUT',
dataType: 'json',
data : {
name: $(this).parent().find('input').val()
},
url : '/names/'+$(this).parents('li').data('id'),
success : function(resp)
{
renderNames(resp);
}
});
e.preventDefault();
});
$('a.list').click(function(e)
{
findAll();
e.preventDefault();
});
 
$("form").submit(function(e)
{
$.post("/names",
{name: $("#newname").val() },
function(resp) {
findAll();
});
e.preventDefault();
});
 
});
 
function findAll()
{
$.get("/names",function(resp)
{
renderNames(resp);
},"json");
};
function renderNames(names)
{
$("#names").empty();
$.each(names,function(k,v)
{
$("#names").append('<li id="name_'+v.id+'" data-id="'+v.id+'"><a class="edit">'+v.name+'</a>'+
'<small><a href="#" class="delete">delete</a></small></li>');
});
}
 
</script>
 
</head>
<body>
<div id="container">
<h3> Names <a class="list" href="#">Load</a></h3>
<ul id="names"></ul>
<form class="" id="newnamefrm">
<input type="text" id="newname"></input><input type="submit" value="Add Name" />
</form>
</div>
</body>
 
</html>
home.tpl.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
<html>
<head>
<title>Names Hello</title>
<script type="text/javascript" src="/js/jquery-1.5.1.js"></script>
<style>
*
{
 
margin: 0 0;
padding: 0 0;
font-family: helvetica, arial, sans-serif;
}
body {
 
background: #CCC;
}
#container
{
padding: 20px;
margin: 0 auto;
width: 600px;
background: #FFF;
}
 
#names
{
width:150px;
}
.delete
{
margin-top:4px;
font-size: 9px;
float: right;
}
 
</style>
<script>
$(function()
{
$("#names").delegate('.edit','click',function(e)
{
$(this).hide()
$(this).parent().append('<input type="text" value="'+$(this).text()+'"></input><button class="save">save</button>');
});
$("#names").delegate('a.delete','click',function(e)
{
$.ajax({type: 'DELETE',
dataType: 'json',
url: '/names/'+$(this).parents('li').data('id'),
success: function(resp)
{
renderNames(resp);
}});
e.preventDefault();
});
$("#names").delegate('button.save','click',function(e)
{
$.ajax({type: 'PUT',
dataType: 'json',
data : {
name: $(this).parent().find('input').val()
},
url : '/names/'+$(this).parents('li').data('id'),
success : function(resp)
{
renderNames(resp);
}
});
e.preventDefault();
});
$('a.list').click(function(e)
{
findAll();
e.preventDefault();
});
 
$("form").submit(function(e)
{
$.post("/names",
{name: $("#newname").val() },
function(resp) {
findAll();
});
e.preventDefault();
});
 
});
 
function findAll()
{
$.get("/names",function(resp)
{
renderNames(resp);
},"json");
};
function renderNames(names)
{
$("#names").empty();
$.each(names,function(k,v)
{
$("#names").append('<li id="name_'+v.id+'" data-id="'+v.id+'"><a class="edit">'+v.name+'</a>'+
'<small><a href="#" class="delete">delete</a></small></li>');
});
}
 
</script>
 
</head>
<body>
<div id="container">
<h3> Names <a class="list" href="#">Load</a></h3>
<ul id="names"></ul>
<form class="" id="newnamefrm">
<input type="text" id="newname"></input><input type="submit" value="Add Name" />
</form>
</div>
</body>
 
</html>
index.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
<?php
require '../Slim/Slim.php';
require '../Name.php';
 
function json($obj)
{
header('Content-Type', 'application/json');
return json_encode($obj);
}
 
$app = new Slim();
$app->config(array('templates.path' => '../templates'));
 
$app->get('/names/:id',function($id) {
echo json(Name::find($id));
});
$app->get('/names',function() {
echo json(Name::findAll());
});
$app->post('/names',function() use ($app) {
$n = new Name(null,$app->request()->post('name'));
$n->create();
echo '{status: "success" }';
});
$app->put('/names/:id',function($id) use ($app) {
$name = Name::find($id);
$name->name = $app->request()->put('name');
$name->update();
echo json(Name::findAll());
});
$app->delete('/names/:id', function($id) use ($app) {
Name::delete($id);
echo json(Name::findAll());
});
 
$app->get('/', function() use($app) {
$app->render('home.tpl.php');
});
 
$app->run();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.