LinkedNestBlog

Codeigniter Core

by 이진원Dev
Codeigniter Core

Ref.] Core 확장
https://opentutorials.org/course/697/3985

  • Core란?
    • Codeigniter 측 코드. 프레임워크에서 기본적으로 동작하는 로직을 의미.
    • Core가 모든 특수한 상황에서 적합하지는 않아 Core를 수정해야 하는 상황이 발생
      • Core를 확장해서 Customizing하는 방법을 제공
        • 방법
          1. Core를 상속
          2. Hook
  • 상속(Extends)
    • 객체지향의 개념.
    • class로 만들어진 library, controller, model 등은 core의 기능을 상속하면서 필요에 따라 동작방법을 수정할 수도.
  • Core의 위치와 Customized Core의 위치
    • Core의 위치
      • system/core/
    • Customized Core
      • application/core/
  • Core의 확장 예
    • CI_Controller를 상속받는 My_Controller의 생성
      • application/core/My_Controller.php 소스코드
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        <?php
        class My_Controller extends CI_Controller{
             function __construct() {
                 parent::__construct();
             }
         
             function _footer(){
                 $this -> load -> view('article/footer');
             }
        }
        ?>
        cs
      • application/controller/article.php 소스코드
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        49
        50
        51
        52
        53
        54
        55
        56
        57
        58
        59
        60
        61
        62
        63
        64
        65
        66
        67
        68
        69
        70
        71
        72
        73
        74
        75
        76
        77
        78
        79
        80
        81
        82
        83
        84
        85
        86
        87
        88
        89
        90
        91
        92
        93
        94
        95
        96
        97
        98
        99
        100
        101
        102
        103
        104
        105
        <?php
        defined('BASEPATH') OR exit('No direct script access allowed');
         
        //Controller : Url 을 mapping 하는 역할
        class Article extends My_Controller {
         
            // 생성자
            function __construct(){
                parent::__construct();    
                // database library  로드
                $this -> load -> database();
                // model 로드
                $this -> load -> model('article_model');
         
                $this -> load -> library('session');
            }
         
         
            // URL : http://127.0.0.1:8080/index.php/topic
            public function index()
            {
                $this -> session -> all_userdata();    
                //log_message('debug', 'all_userdata() : '.var_dump($this -> session -> all_userdata()));
         
                $this -> _header();
                $this -> load ->view('article/main');
                #$this -> load ->view('article/footer');
                $this -> _footer();
            }
         
            // URL : http://127.0.0.1:8080/index.php/topic/get/3
            // Parameter : $id = 3
            public function get($id){
                $this -> _header();
                // 특정 article 조회
                $article = $this -> article_model -> getArticle($id);    
                // helper의 사용
                $this->load->helper(array('url', 'HTML', 'korean'));
                // article view
                $this -> load -> view('article/get', array('article'=>$article));
                #$this -> load -> view('article/footer');
                $this -> _footer();
            }
            
            // description : header에 출력되는, 공통 사항들을 별도의 메서드로 분리함.
            function _header(){
                $this -> load ->view('article/header');
                // article list 조회
                $article_list = $this -> article_model -> getArticles();
                // article list view 
                $this -> load -> view('article/article_list', array('articles' => $article_list));
            }
         
            // description : form validation을 사용
            function add(){
                $this -> _header();
                // library load
                $this -> load -> library('form_validation');
                // class의 method 사용
                $this -> form_validation -> set_rules('title', '제목', 'required');
                $this -> form_validation -> set_rules('content', '내용', 'required'); 
         
                if($this -> form_validation -> run() == FALSE){
                    // validation 결과가 false이면 write 화면으로
                    $this -> load -> view('article/add');
                }else{
                    // validation 결과가 true이면 
                    // 1. DB 입력 수행
                    $article_id = $this -> article_model -> add($this -> input -> post('title'), $this -> input -> post('content'));
         
                    // 2. get(view) 화면으로  redirect
                    $this -> load ->helper('url');
                    redirect('/article/get/'.$article_id);
                }
                #$this -> load -> view('footer');
                $this -> _footer();
            }
         
            // ckeditor 파일 업로드
            public function upload(){
                $config['upload_path'] = "./static/images";
                $config['allowed_types'] = "gif|jpg|png|jpeg";
                $config['max_size'] = "10000";
                $config['max_width'] = "1024";
                $config['max_height'] = "768";
                $this -> load -> library('upload', $config);
         
                // 2. 파일 업로드
                if( $this -> upload -> do_upload('upload') ){
                    $CKEditorFunctionNum = $this -> input -> get('CKEditorFuncNum');    
         
                    $data = $this -> upload -> data();
                    $filename = $data['file_name'];
         
                    $url = '/static/images/'.$filename;
                    // 1. CKEditorFunction 번호
                    // 2. url : 이미지 url
                    // 3. 전송 성공 message
                    echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction('".$CKEditorFunctionNum."', '".$url."', '전송 성공 ')</script>";
                }else{
                    echo "<script>alert('업로드 실패".$this -> upload ->display_errors('','')."');</script>";
                }
            }
         
        }
        cs
Codeigniter Core · LinkedNest Blog