質問編集履歴
2
微妙追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -253,3 +253,169 @@
|
|
253
253
|
}
|
254
254
|
|
255
255
|
```
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
。。。。微妙。。。。
|
262
|
+
|
263
|
+
```php
|
264
|
+
|
265
|
+
<?php
|
266
|
+
|
267
|
+
defined('BASEPATH') OR exit('のび太さんのエッチ!');
|
268
|
+
|
269
|
+
class Api extends CI_Controller{
|
270
|
+
|
271
|
+
function __construct()
|
272
|
+
|
273
|
+
{
|
274
|
+
|
275
|
+
parent::__construct();
|
276
|
+
|
277
|
+
$this->load->model('api_model');
|
278
|
+
|
279
|
+
$this->load->library('form_validation');
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
public function add_user()
|
286
|
+
|
287
|
+
{
|
288
|
+
|
289
|
+
$json_data = $this->validation_set_value();
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
$result = $this->error_check($json_data)
|
294
|
+
|
295
|
+
if ( ! empty($result) )
|
296
|
+
|
297
|
+
{
|
298
|
+
|
299
|
+
$result = new stdClass;
|
300
|
+
|
301
|
+
$result->statusCode = 'success!!!';
|
302
|
+
|
303
|
+
$result->message = 'ユーザー登録できたっぽいよ!';
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
return $this->show_output($result);
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
private function show_output(array $result)
|
314
|
+
|
315
|
+
{
|
316
|
+
|
317
|
+
return $this->output
|
318
|
+
|
319
|
+
->set_content_type('application/json')
|
320
|
+
|
321
|
+
->set_output(json_encode($result));
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
private function error_check($json_data)
|
328
|
+
|
329
|
+
{
|
330
|
+
|
331
|
+
if ( $this->fvalidation_check($json_data) === FALSE )
|
332
|
+
|
333
|
+
{
|
334
|
+
|
335
|
+
$result = $result = $this->err('Request Validation Error', 1);
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
elseif ( ! $this->api_model->add_user($json_data) )
|
340
|
+
|
341
|
+
{
|
342
|
+
|
343
|
+
$result = $this->err('User add Error', 1);
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
$result;
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
private function err($msg, $code)
|
354
|
+
|
355
|
+
{
|
356
|
+
|
357
|
+
$result = new stdClass;
|
358
|
+
|
359
|
+
$result->statusCode = 'EA' . $e->getCode();
|
360
|
+
|
361
|
+
$result->message = $e->getMessage();
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
if ( ! empty(validation_errors()) )
|
366
|
+
|
367
|
+
{
|
368
|
+
|
369
|
+
$result->validationMessages = $this->form_validation->error_array();
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
return $result;
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
private function validation_check()
|
380
|
+
|
381
|
+
{
|
382
|
+
|
383
|
+
$this->form_validation->set_data($json_data);
|
384
|
+
|
385
|
+
$this->form_validation->set_rules('name', 'lang:name', 'required|max_length[100]');
|
386
|
+
|
387
|
+
$this->form_validation->set_rules('tel', 'lang:tel', 'required|is_natural|max_length[13]');
|
388
|
+
|
389
|
+
$this->form_validation->set_rules('sex', 'lang:sex', 'required|in_list[1,2]');
|
390
|
+
|
391
|
+
return $this->form_validation->run();
|
392
|
+
|
393
|
+
}
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
private function validation_set_value()
|
398
|
+
|
399
|
+
{
|
400
|
+
|
401
|
+
//POST Request以外は無視
|
402
|
+
|
403
|
+
if ( $this->input->method(TRUE) !== 'POST' ) return show_404();
|
404
|
+
|
405
|
+
$json_str = $this->input->raw_input_stream;
|
406
|
+
|
407
|
+
//ここform_validationしやすい様に配列で渡す。
|
408
|
+
|
409
|
+
$json_data = json_decode($json_str, TRUE);
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
if ( empty($json_data) ) return show_404();
|
414
|
+
|
415
|
+
return $json_data;
|
416
|
+
|
417
|
+
}
|
418
|
+
|
419
|
+
}
|
420
|
+
|
421
|
+
```
|
1
if 分岐パターン追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -141,3 +141,115 @@
|
|
141
141
|
}
|
142
142
|
|
143
143
|
```
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
大げさですがifで分岐するパターンです。
|
150
|
+
|
151
|
+
```php
|
152
|
+
|
153
|
+
<?php
|
154
|
+
|
155
|
+
defined('BASEPATH') OR exit('のび太さんのエッチ!');
|
156
|
+
|
157
|
+
class Api extends CI_Controller{
|
158
|
+
|
159
|
+
function __construct()
|
160
|
+
|
161
|
+
{
|
162
|
+
|
163
|
+
parent::__construct();
|
164
|
+
|
165
|
+
$this->load->model('api_model');
|
166
|
+
|
167
|
+
$this->load->library('form_validation');
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
public function add_user()
|
174
|
+
|
175
|
+
{
|
176
|
+
|
177
|
+
//POST Request以外は無視
|
178
|
+
|
179
|
+
if ( $this->input->method(TRUE) !== 'POST' ) return show_404();
|
180
|
+
|
181
|
+
$json_str = $this->input->raw_input_stream;
|
182
|
+
|
183
|
+
//ここform_validationしやすい様に配列で渡す。
|
184
|
+
|
185
|
+
$json_data = json_decode($json_str, TRUE);
|
186
|
+
|
187
|
+
if ( empty($json_data) ) return show_404();
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
$this->form_validation->set_data($json_data);
|
192
|
+
|
193
|
+
$this->form_validation->set_rules('name', 'lang:name', 'required|max_length[100]');
|
194
|
+
|
195
|
+
$this->form_validation->set_rules('tel', 'lang:tel', 'required|is_natural|max_length[13]');
|
196
|
+
|
197
|
+
$this->form_validation->set_rules('sex', 'lang:sex', 'required|in_list[1,2]');
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
$result = new stdClass;
|
202
|
+
|
203
|
+
if ( $this->form_validation->run() === FALSE )
|
204
|
+
|
205
|
+
{
|
206
|
+
|
207
|
+
$result->message = 'Request Validation Error';
|
208
|
+
|
209
|
+
$result->statusCode = 'EA2';
|
210
|
+
|
211
|
+
$result->validationMessages = $this->form_validation->error_array();
|
212
|
+
|
213
|
+
return $this->output
|
214
|
+
|
215
|
+
->set_content_type('application/json')
|
216
|
+
|
217
|
+
->set_output(json_encode($result));
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
if ( ! $this->api_model->add_user($json_data) )
|
224
|
+
|
225
|
+
{
|
226
|
+
|
227
|
+
$result->message = 'User add Error';
|
228
|
+
|
229
|
+
$result->statusCode = 'EA1';
|
230
|
+
|
231
|
+
return $this->output
|
232
|
+
|
233
|
+
->set_content_type('application/json')
|
234
|
+
|
235
|
+
->set_output(json_encode($result));
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
$result->statusCode = 'success!!!';
|
242
|
+
|
243
|
+
$result->message = 'ユーザー登録できたっぽいよ!';
|
244
|
+
|
245
|
+
return $this->output
|
246
|
+
|
247
|
+
->set_content_type('application/json')
|
248
|
+
|
249
|
+
->set_output(json_encode($result));
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
```
|