간단한 백엔드 만들기 클래스
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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5186 | PHP |
swallow
|
2년 전 | 940 | |
| 5185 | PHP |
swallow
|
2년 전 | 960 | |
| 5184 | PHP |
swallow
|
2년 전 | 536 | |
| 5183 | PHP |
swallow
|
2년 전 | 840 | |
| 5182 | PHP |
swallow
|
2년 전 | 775 | |
| 5181 | 기타 |
swallow
|
2년 전 | 1017 | |
| 5180 | 2년 전 | 573 | |||
| 5179 | 기타 |
swallow
|
2년 전 | 1430 | |
| 5178 | PHP |
swallow
|
2년 전 | 794 | |
| 5177 | JavaScript |
swallow
|
2년 전 | 1487 | |
| 5176 | PHP |
swallow
|
2년 전 | 1183 | |
| 5175 | 2년 전 | 564 | |||
| 5174 | 기타 | 2년 전 | 810 | ||
| 5173 | PHP |
|
2년 전 | 964 | |
| 5172 | PHP |
|
2년 전 | 1251 | |
| 5171 | 기타 |
그누GPT
|
2년 전 | 1552 | |
| 5170 | PHP |
|
2년 전 | 1047 | |
| 5169 | PHP |
|
2년 전 | 1222 | |
| 5168 | PHP |
|
2년 전 | 1197 | |
| 5167 | JavaScript |
|
2년 전 | 994 | |
| 5166 | MySQL | 2년 전 | 1184 | ||
| 5165 | MySQL | 2년 전 | 1250 | ||
| 5164 | PHP | 2년 전 | 1499 | ||
| 5163 | OS |
|
2년 전 | 839 | |
| 5162 | 웹서버 |
|
2년 전 | 888 | |
| 5161 | 웹서버 |
|
2년 전 | 972 | |
| 5160 | PHP |
|
2년 전 | 842 | |
| 5159 | PHP |
|
2년 전 | 903 | |
| 5158 | PHP |
|
2년 전 | 912 | |
| 5157 | PHP |
|
2년 전 | 941 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기