Repository navigation
Expand file tree
/
Copy pathrest_server.php
More file actions
222 lines (187 loc) · 6.54 KB
/
Copy pathrest_server.php
File metadata and controls
222 lines (187 loc) · 6.54 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>REST Server Tests</title>
<style>
::selection { background-color: #E13300; color: white; }
::-moz-selection { background-color: #E13300; color: white; }
body {
background-color: #FFF;
margin: 40px;
font: 16px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
word-wrap: break-word;
}
a {
color: #039;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 24px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 16px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#body {
margin: 0 15px 0 15px;
}
p.footer {
text-align: right;
font-size: 16px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}
#container {
margin: 10px;
border: 1px solid #D0D0D0;
box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>
<div id="container">
<h1>REST Server Tests</h1>
<div id="body">
<h2><a href="<?php echo site_url(); ?>">Home</a></h2>
<p>
See the article
<a href="http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/" target="_blank">
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/
</a>
</p>
<p>
The master project repository is
<a href="https://github.com/chriskacerguis/codeigniter-restserver" target="_blank">
https://github.com/chriskacerguis/codeigniter-restserver
</a>
</p>
<p>
Click on the links to check whether the REST server is working.
</p>
<ol>
<li><a href="<?php echo site_url('api/example/users'); ?>">Users</a> - defaulting to JSON</li>
<li><a href="<?php echo site_url('api/example/users/format/csv'); ?>">Users</a> - get it in CSV</li>
<li><a href="<?php echo site_url('api/example/users/id/1'); ?>">User #1</a> - defaulting to JSON (users/id/1)</li>
<li><a href="<?php echo site_url('api/example/users/1'); ?>">User #1</a> - defaulting to JSON (users/1)</li>
<li><a href="<?php echo site_url('api/example/users/id/1.xml'); ?>">User #1</a> - get it in XML (users/id/1.xml)</li>
<li><a href="<?php echo site_url('api/example/users/id/1/format/xml'); ?>">User #1</a> - get it in XML (users/id/1/format/xml)</li>
<li><a href="<?php echo site_url('api/example/users/id/1?format=xml'); ?>">User #1</a> - get it in XML (users/id/1?format=xml)</li>
<li><a href="<?php echo site_url('api/example/users/1.xml'); ?>">User #1</a> - get it in XML (users/1.xml)</li>
<li><a id="ajax" href="<?php echo site_url('api/example/users/format/json'); ?>">Users</a> - get it in JSON (AJAX request)</li>
<li><a href="<?php echo site_url('api/example/users.html'); ?>">Users</a> - get it in HTML (users.html)</li>
<li><a href="<?php echo site_url('api/example/users/format/html'); ?>">Users</a> - get it in HTML (users/format/html)</li>
<li><a href="<?php echo site_url('api/example/users?format=html'); ?>">Users</a> - get it in HTML (users?format=html)</li>
</ol>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>'.CI_VERSION.'</strong>' : '' ?></p>
</div>
<script src="https://code.jquery.com/jquery-1.12.0.js"></script>
<script>
// Create an 'App' namespace
var App = App || {};
// Basic rest module using an IIFE as a way of enclosing private variables
App.rest = (function restModule(window) {
// Fields
var _alert = window.alert;
var _JSON = window.JSON;
// Cache the jQuery selector
var _$ajax = null;
// Cache the jQuery object
var $ = null;
// Methods (private)
/**
* Called on Ajax done
*
* @return {undefined}
*/
function _ajaxDone(data) {
// The 'data' parameter is an array of objects that can be iterated over
_alert(_JSON.stringify(data, null, 2));
}
/**
* Called on Ajax fail
*
* @return {undefined}
*/
function _ajaxFail() {
_alert('Oh no! A problem with the Ajax request!');
}
/**
* On Ajax request
*
* @param {jQuery} $element Current element selected
* @return {undefined}
*/
function _ajaxEvent($element) {
$.ajax({
// URL from the link that was 'clicked' on
url: $element.attr('href')
})
.done(_ajaxDone)
.fail(_ajaxFail);
}
/**
* Bind events
*
* @return {undefined}
*/
function _bindEvents() {
// Namespace the 'click' event
_$ajax.on('click.app.rest.module', function (event) {
event.preventDefault();
// Pass this to the Ajax event function
_ajaxEvent($(this));
});
}
/**
* Cache the DOM node(s)
*
* @return {undefined}
*/
function _cacheDom() {
_$ajax = $('#ajax');
}
// Public API
return {
/**
* Initialise the following module
*
* @param {object} jQuery Reference to jQuery
* @return {undefined}
*/
init: function init(jQuery) {
$ = jQuery;
// Cache the DOM and bind event(s)
_cacheDom();
_bindEvents();
}
};
}(window));
// DOM ready event
$(function domReady($) {
// Initialise the App module
App.rest.init($);
});
</script>
</body>
</html>