LinkedNestBlog

Codeigniter View

by 이진원Dev
Codeigniter View


Ref.] https://opentutorials.org/course/697/3826

  • View의 사용 목적
    • Controller와 View의 분리 
      • 시각적인 부분과 논리적인 부분을 분리
    • 자원을 차계적으로 관리하기 위함
  • View의 경로
    • /Applications/mampstack-5.6.20-0/apache2/htdocs/application/views
  • 예를 들어
    • URL
      • http://127.0.0.1:8080/index.php/topic
      • http://127.0.0.1:8080/index.php/topic/get/2
    •  Controller
      • 설명
        • echo로 호출하던 부분을 $this->load->view 라는 구문으로 대체
        • $this->load->view('확장자를 제외한 파일이름', 데이터 배열)의 형식을 이용해서 데이터를 view로 전달 
      • 소스코드
        • topic.php 
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
           
          <?php
          defined('BASEPATH') OR exit('No direct script access allowed');
           
          //Controller : Url 을 mapping 하는 역할
          class Topic extends CI_Controller {
           
              // http://127.0.0.1:8080/index.php/topic
              public function index()
              {
                  #echo 'topic page';
                  $this -> load ->view('topic');
              }
           
              // http://127.0.0.1:8080/index.php/topic/get/3
              // $id = 3
              public function get($id){
                  #echo "topic".$id;
                  $this -> load ->view('get', array('id'=>$id));
              }
          }
           
          cs
    • View 
      • 소스코드
        • topic.php 
          1
          2
          3
          4
          5
          6
          7
          8
          9
          <!DOCTYPE html>
          <html>
              <head>
                  <meta charset="utf-8"/>
              </head>
              <body>
                      this is topic page!
              </body>
          </html>
          cs
        • get.php 
          1
          2
          3
          4
          5
          6
          7
          8
          9
          <!DOCTYPE html>
          <html>
              <head>
                  <meta charset="utf-8"/>
              </head>
              <body>
                      this is topic <?=$id?>!
              </body>
          </html>
          cs
    • view load시 파라미터의 전달
      • 설명
        • Controller에서 View가 되는 php파일을 load할 때, 데이터가 포함된 view를 load하는 경우, 아래와 같은 형태로 파라미터가 삽입되어야 한다.
      • 형태
        $this -> load ->view(url, array 형태의 parameter);
    • view load 시 html 출력 여부 옵션
      • 설명
        • Controller 메서드 내에서 view를 load하지만 바로 출력하지 않고 가장 끝에서 load 해서 출력할 view에 포함해서 html을 출력할 필요가 있을 때 사용.
      • 형태
        $this -> load ->view(url, array 형태의 parameter, html 바로 출력 여부 값(boolean type))