간단한 백엔드 만들기 클래스
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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4946 | node.js | 6년 전 | 2614 | ||
| 4945 | node.js | 6년 전 | 2386 | ||
| 4944 | node.js | 6년 전 | 2524 | ||
| 4943 | node.js | 6년 전 | 2276 | ||
| 4942 | node.js | 6년 전 | 2256 | ||
| 4941 | node.js | 6년 전 | 2723 | ||
| 4940 | node.js | 6년 전 | 1869 | ||
| 4939 | node.js | 6년 전 | 1996 | ||
| 4938 | node.js | 6년 전 | 2460 | ||
| 4937 | node.js | 6년 전 | 2253 | ||
| 4936 | node.js | 6년 전 | 2326 | ||
| 4935 | node.js | 6년 전 | 2139 | ||
| 4934 | node.js | 6년 전 | 2446 | ||
| 4933 | node.js | 6년 전 | 2250 | ||
| 4932 | node.js | 6년 전 | 2690 | ||
| 4931 | node.js | 6년 전 | 2073 | ||
| 4930 | node.js | 6년 전 | 2000 | ||
| 4929 | node.js | 6년 전 | 8636 | ||
| 4928 | node.js | 6년 전 | 3754 | ||
| 4927 | node.js | 6년 전 | 2397 | ||
| 4926 | node.js | 6년 전 | 2507 | ||
| 4925 | node.js | 6년 전 | 2088 | ||
| 4924 | node.js | 6년 전 | 3376 | ||
| 4923 | node.js | 6년 전 | 2225 | ||
| 4922 | node.js | 6년 전 | 1995 | ||
| 4921 | node.js | 6년 전 | 2049 | ||
| 4920 | node.js | 6년 전 | 1768 | ||
| 4919 | node.js | 6년 전 | 2034 | ||
| 4918 | node.js | 6년 전 | 2182 | ||
| 4917 | node.js | 6년 전 | 2398 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기