간단한 백엔드 만들기 클래스
vue 프론트엔드 작업중인데 백엔드가 완성되지 않아서 목업api로 사용하려고 만들어 봤습니다.
간단하게 백엔드를 구성할 수 있습니다.
<?php
class App {
private $params = array();
// 생성자 (CORS 처리)
public function __construct(){
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}
// OPTIONS 요청일때
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
}
// json 타입으로 결과 뿌리는 함수
public function print($arr, $code=200) {
http_response_code($code);
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
exit;
}
// 파라미터 가져오기 (post, patch, put... 모두 이걸로 얻어옴)
public function getData() {
return json_decode(file_get_contents('php://input'), true);
}
// URL 파라미터 구해오기
public function getParams() {
return $this->params;
}
// URL 분석
private function route($pat) {
$pat = '~^'.$pat.'$~i';
$request = $_SERVER['REQUEST_URI'];
$ret = preg_match($pat, $request, $mat);
if (count($mat) > 1) {
array_shift($mat);
$this->params = $mat;
}
return $ret;
}
// METHOD 체크
private function checkMethod($method) {
$_method = strtolower($_SERVER["REQUEST_METHOD"]);
if ($_method !== $method) return false;
return true;
}
// GET 요청 분석
public function get($pat) {
if(!$this->checkMethod('get')) return false;
return $this->route($pat);
}
// POST 요청 분석
public function post($pat) {
if(!$this->checkMethod('post')) return false;
return $this->route($pat);
}
// PUT 요청 분석
public function put($pat) {
if(!$this->checkMethod('put')) return false;
return $this->route($pat);
}
// PATCH 요청 분석
public function patch($pat) {
if(!$this->checkMethod('patch')) return false;
return $this->route($pat);
}
// DELETE 요청 분석
public function delete($pat) {
if(!$this->checkMethod('delete')) return false;
return $this->route($pat);
}
}
<< 사용방법 >>
//--------------------------
// 객체생성
//--------------------------
$app = new App();
//--------------------------
// 라우터
//--------------------------
/*
POST
*/
if ($app->post('/signin')) {
// URL 파라미터 구해오기(정규식부분에서 괄호 부분 순서대로 가져옴)
$params = $app->getParams();
// POST, PUT 등에서 보내온 데이타
$data = $app->getData();
//echo $data['user_id'];
//echo $data['password'];
// 결과출력
$app->print(array(
'user_id' => 'melong'.
'name' => '메롱'
));
/*
GET
*/
} else if ($app->get('/users/([a-zA-Z0-9_])')) {
/* "/user_id/melong" 을 호출시 */
//echo $params[0]; // melong
/*
PUT
*/
} else if ($app->put('/users/([a-zA-Z0-9])')) {
//echo $data['user_id'];
//echo $data['password'];
//echo $params[0];
/*
PATCH
*/
} else if ($app->patch('/users/([a-zA-Z0-9])')) {
//
/*
DELETE
*/
} else if($app->delete('/users/([a-zA-Z0-9])')) {
//echo $params[0];
/*
여러개의 URL 파라미터
*/
} else if ($app->get('/article/([0-9])/([0-9])/edit')) {
//echo $params[0];
//echo $params[1];
}
댓글 1개
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5126 | PHP |
|
3년 전 | 1801 | |
| 5125 | PHP | 3년 전 | 1608 | ||
| 5124 | PHP | 3년 전 | 2122 | ||
| 5123 | OS | 3년 전 | 1403 | ||
| 5122 | OS | 3년 전 | 1323 | ||
| 5121 | OS | 3년 전 | 1423 | ||
| 5120 | OS | 3년 전 | 1299 | ||
| 5119 | PHP |
|
3년 전 | 1152 | |
| 5118 | PHP | 3년 전 | 1371 | ||
| 5117 | Mobile |
|
3년 전 | 1410 | |
| 5116 | PHP | 3년 전 | 2302 | ||
| 5115 | MySQL |
welcome
|
3년 전 | 2910 | |
| 5114 | OS | 3년 전 | 2010 | ||
| 5113 | JavaScript | 3년 전 | 1543 | ||
| 5112 | PHP | 3년 전 | 1563 | ||
| 5111 | 기타 |
|
3년 전 | 1303 | |
| 5110 | PHP | 3년 전 | 4547 | ||
| 5109 | PHP | 3년 전 | 1583 | ||
| 5108 | 기타 |
|
3년 전 | 12179 | |
| 5107 | 기타 |
|
3년 전 | 2414 | |
| 5106 | 기타 |
|
3년 전 | 6325 | |
| 5105 | 기타 |
|
3년 전 | 3700 | |
| 5104 | 기타 |
|
3년 전 | 1508 | |
| 5103 | JavaScript | 3년 전 | 1700 | ||
| 5102 | 기타 |
|
3년 전 | 1274 | |
| 5101 | 기타 |
|
3년 전 | 1472 | |
| 5100 | 기타 |
|
3년 전 | 1312 | |
| 5099 | 웹서버 | 3년 전 | 2186 | ||
| 5098 | PHP | 3년 전 | 1335 | ||
| 5097 | PHP |
|
3년 전 | 1992 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기