간단한 백엔드 만들기 클래스
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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4856 | PHP | 7년 전 | 5730 | ||
| 4855 | jQuery | 7년 전 | 3234 | ||
| 4854 | jQuery | 7년 전 | 2225 | ||
| 4853 | jQuery | 7년 전 | 2825 | ||
| 4852 | MySQL | 7년 전 | 2993 | ||
| 4851 | jQuery | 7년 전 | 2339 | ||
| 4850 | jQuery | 7년 전 | 2569 | ||
| 4849 | jQuery | 7년 전 | 4307 | ||
| 4848 | PHP |
|
7년 전 | 4246 | |
| 4847 | jQuery | 7년 전 | 4019 | ||
| 4846 | jQuery | 7년 전 | 2629 | ||
| 4845 | jQuery | 7년 전 | 2151 | ||
| 4844 | jQuery | 7년 전 | 2298 | ||
| 4843 | jQuery | 7년 전 | 3053 | ||
| 4842 | jQuery | 7년 전 | 2898 | ||
| 4841 | jQuery | 7년 전 | 2228 | ||
| 4840 | jQuery | 7년 전 | 1565 | ||
| 4839 | jQuery | 7년 전 | 2466 | ||
| 4838 | jQuery |
이에스씨코리아
|
7년 전 | 2311 | |
| 4837 | jQuery | 7년 전 | 2161 | ||
| 4836 | jQuery | 7년 전 | 2086 | ||
| 4835 | jQuery | 7년 전 | 1816 | ||
| 4834 | jQuery | 7년 전 | 1865 | ||
| 4833 | jQuery | 7년 전 | 1992 | ||
| 4832 | jQuery | 7년 전 | 2183 | ||
| 4831 | jQuery | 7년 전 | 2362 | ||
| 4830 | jQuery | 7년 전 | 2629 | ||
| 4829 | jQuery | 7년 전 | 1802 | ||
| 4828 | jQuery | 8년 전 | 2741 | ||
| 4827 | jQuery | 8년 전 | 2281 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기