웹 프레임워크 공부 정보
웹 프레임워크 공부본문
워드 프레스에서 $something->on('...', function ... ); 이런식으로 페이지를 확장하는걸 보고,
이걸 웹 프레임워크 전체에 적용하면 어떻게 되는건가 싶어서 실험 삼아 만들어 보았습니당.
현재 사용자 코드 경로 캐슁을 하나도 안하기 때문에 응답 속도가 느려질 수 있어요.
실무에 적용할 수는 없는 그저 실험용 코드입니다.
코드 리뷰 부탁드립니다!!
테스트용 비지니스(?) 코드
$Core = \gwf\Core::core();
$Core->on('post:init', function(gwf\Route $Router) {
$Router->define('/')
->layout('example')
->content(__DIR__ . '/main.php');
$Router->define('/sub')
->layout('example.sub')
->content(__DIR__ . '/sub.php');
});
테스트용 레이아웃 모듈 코드.
class LayoutModule {
private $Route;
function __construct(gwf\Route $Route) {
$this->Route = $Route;
/* 기본 레이아웃 설정. */
$Route->layout = "example";
$Route->on('route:define', function($Path, $Template) {
/* 레이아웃 설정을 상속 시킵니다. */
$Template->layout = $this->Route->layout;
});
$Route->on('pre:render', function(gwf\Route $Route) {
ob_start();
});
$Route->on('post:render', function(gwf\Route $Route) {
$Contents = ob_get_contents();
ob_end_clean();if (!$Route->contentType || $Route->contentType == 'text/html') {
if (isset($Route->layout) &&
file_exists($Layout = GWF_ROOT . "/layouts/{$Route->layout}.php"))
{
if (!is_array($Route->stylesheets)) {
$Route->stylesheets = [];
}
if (!is_array($Route->scripts)) {
$Route->scripts = [];
}$Title = $Route->title;
$Stylesheets = $Route->stylesheets;
$Scripts = $Route->scripts;ob_start();
include ($Layout);
$Contents = ob_get_contents();
ob_end_clean();
}
}
echo $Contents;
});
}
/*
* 페이지 레이아웃을 지정합니다.
* */
function layout($Name = null) {
if (!$Name) {
if (isset($this->Route->layout))
return $this->Route->layout;
return null;
}
$this->Route->layout = $Name;
return $this->Route;
}
}/* 레이아웃 모듈을 등록합니다. */
\gwf\RouteTemplate::module(LayoutModule::class);
감사합니다!
풀코드: https://drive.google.com/file/d/1j70ITG4X_JTS9KbejTEwlG3mxoZd6n36/view?usp=sharing
0
댓글 2개
알수없음