웹 프레임워크 공부 > 자유게시판

자유게시판

웹 프레임워크 공부 정보

웹 프레임워크 공부

본문

워드 프레스에서 $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개

전체 195,268 |RSS
자유게시판 내용 검색

회원로그인

진행중 포인트경매

  1. 참여5 회 시작24.04.19 15:40 종료24.04.26 15:40
(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT