RestAPI 와 그누보드 코어 연동 정보
RestAPI 와 그누보드 코어 연동본문
그누보드는 명시적으로 core 라고 지칭된 파트가 없습니다.
라이브러리와 함수형태로 코어가 존재합니다.
그누보드 restapi를 만들때, 이 코어 코드를 재설계없이 최대한 사용하여, 코드량을 줄이는게 저의 개발 방향입니다.
(사람마다 다르겠지만, 저는 지금 이 방법이 최선이라고 봅니다.)
자바스크립트를 하시는 분들은 잘 아시겠지만, PHP에도 호이스팅 이란 기능이 존재합니다.
외부에 먼저 선언된 함수는 사용할수 있다게 됩니다. include가 함수 호출전에 실행되었다면, include구문 안에 있는 함수도 사용할수 있습니다. 이건 클래스에서도 마찬가집니다. 그러니까 전역 함수 같은 기능이죠.
그누보드 코어는 전역함라고 보면 될것 같습니다.(common.lib.php, register.lib.php 같은..)
코드로 예를 들어보죠. 회원가입을 하는 경우에, 닉네임 중복, 아이디 중복, 유효문자 등등을 체크해야 합니다. register.lib.php 에 해당 함수들이 위치하고 있습니다. restapi를 만들때, 이걸 재코딩 한다면, 시간이 많이 소요될뿐만 아니라, 보안 관련된 패치에 함수 내용이 달라진다면, 일일이 패치까지 해줘야 하는 번거로움이 발생합니다.
호이스팅 기능을 최대한 사용하여, 코드를 연동시킵니다.
예제 코드입니다.
MemberController.php
<?php
class MemberController extends BaseController
{
protected $router = null;
protected $pdo_db = null;
protected $config = null;
public function __construct($router) {
parent::__construct($router);
}
/**
* 회원가입 처리
* @param $post_param
* @return bool|float|int|mixed|\Services_JSON_Error|string
*/
public function register($post_param) {
try {
$mb_id = clean_xss_tags(trim($post_param['userid']));
$mb_password = clean_xss_tags(trim($post_param['password1']));
$mb_nick = clean_xss_tags(trim($post_param['nick']));
$mb_email = clean_xss_tags(trim($post_param['email']));
if ($msg = empty_mb_id($mb_id)) {
throw new APIException($msg, 400);
}
if ($msg = valid_mb_id($mb_id)) {
throw new APIException($msg, 400);
}
if ($msg = count_mb_id($mb_id)) {
throw new APIException($msg, 400);
}
if ($msg = exist_mb_id($mb_id)) {
throw new APIException($msg, 400);
}
if ($msg = reserve_mb_id($mb_id)) {
throw new APIException($msg, 400);
}
if ($msg = empty_mb_nick($mb_nick)) {
throw new APIException($msg, 400);
}
if ($msg = empty_mb_email($mb_email)) {
throw new APIException($msg, 400);
}
if ($msg = reserve_mb_nick($mb_nick)) {
throw new APIException($msg, 400);
}
// 이름에 한글명 체크를 하지 않는다.
if ($msg = valid_mb_nick($mb_nick)) {
throw new APIException($msg, 400);
}
if ($msg = exist_mb_nick($mb_nick, $mb_id)) {
throw new APIException($msg, 400);
}
if ($msg = exist_mb_nick($mb_nick, $mb_id)) {
throw new APIException($msg, 400);
}
//....
//....
//....
return $this->router->withJson(['message' => 'OK', 'code' => 200], 200);
} catch (APIException $ex) {
return $this->router->withJson(['message' => $ex->getMessage(), 'code' => $ex->getCode()], 200);
}
}
}
Validation(유효성 체크) 은 try catch, throw Exception 이 좋은 형태입니다. class로만 구현한 코드보다 더 깔끔하고
가독성 높은 코드로 보여집니다.
MemberController 를 최초 생성하는 index.php 는 이런 형태죠.
index.php
require dirname(__DIR__) . '/common.php'; //그누보드 코어 코드 include
require G5_LIB_PATH . '/lib/register.lib.php';
//...
$router->mount('/member', function () use ($router, $db) {
$controller = new MemberController($router);
$router->post('/login', function () use ($controller) {
return $controller->login($_POST);
});
$router->post('/logout', function () use ($controller) {
return $controller->logout();
});
$router->post('/register', function () use ($controller) {
return $controller->register($_POST);
});
//.....
});
그누보드 관리자에서 설정한 가입 정책도 문제없이 체크됩니다.
!-->!-->
0
댓글 5개
차라리 slim 4를 사용하는 것이 나중을 위해서 더 편하지 않을까요?