Project
— Overview
생명과학 연구기관을 위한 다국어 콘텐츠 관리 시스템. 연구 성과·논문·뉴스를 한국어·영어로 동시에 관리하며, 관리자 페이지에서 콘텐츠를 WYSIWYG 에디터로 작성하면 프론트엔드에 즉시 반영된다.
PHP MVC 패턴으로 구현해 라우팅·비즈니스 로직·뷰를 명확히 분리했다. 언어 전환은 세션 기반으로 처리하고, 번역 문자열은 언어별 PHP 배열 파일로 관리한다.
— Core Logic
프론트 컨트롤러 패턴의 라우터가 URL을 정규식으로 매칭해 해당 컨트롤러와 액션으로 디스패치한다.
class Router
{
private array $routes = [];
public function get(string $pattern, string $controller, string $action): void
{
$this->routes[] = ['GET', $pattern, $controller, $action];
}
public function dispatch(string $uri): void
{
foreach ($this->routes as [$method, $pattern, $ctrl, $action]) {
if ($_SERVER['REQUEST_METHOD'] !== $method) continue;
if (preg_match($pattern, $uri, $matches)) {
(new $ctrl)->$action($matches);
return;
}
}
(new ErrorController)->notFound();
}
}
// index.php
$router->get('#^/research/(\d+)$#', ResearchController::class, 'show');
$router->get('#^/news$#', NewsController::class, 'index');core/Router.php — front controller dispatch
t() 헬퍼 함수가 현재 세션 언어에 맞는 문자열 파일을 lazy load하고
키로 번역 문자열을 반환한다.
function t(string $key, array $params = []): string
{
static $strings = null;
if ($strings === null) {
$lang = $_SESSION['lang'] ?? 'ko';
$file = __DIR__ . "/lang/{$lang}.php";
$strings = file_exists($file) ? require $file : [];
}
$text = $strings[$key] ?? $key; // 미번역 키는 원문 그대로 출력
// 플레이스홀더 치환: t('welcome', ['name' => 'Luka'])
foreach ($params as $k => $v) {
$text = str_replace(":{$k}", $v, $text);
}
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}helpers/i18n.php — translation helper
— How It Works
index.php(프론트 컨트롤러)를 거쳐 라우터로 전달된다.t('key')를 호출하면 세션에 저장된 언어(ko/en)에 맞는 문자열을 반환한다.$_SESSION['lang']을 변경하고 현재 페이지로 리다이렉트해 전체 UI가 즉시 전환된다.