카테고리 없음
CodeIgniter 뷰 (Views)
광82
2023. 7. 12. 11:29
반응형
설명
이제 MVC 모델에서 뷰 (Views)에 대해 알아보도록 하겠습니다.
- 뷰는 웹페이지에서 실제로 사용자에게 보여주는 부분입니다. (HTML 코드가 위치)
- 뷰는 웹페이지에 조각입니다. (컨트롤러에서 여러 개의 뷰를 호출할 수 있습니다.)
- 뷰는 컨트롤러(Controller)를 통해서 호출합니다.
이전 예제를 기준으로 사용법을 설명하도록 하겠습니다.
▶뷰 생성 및 호출
뷰 생성 (Creating a View)
application/views/index.php
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Tutorial::index</title>
</head>
<body>
Hello Index
</body>
</html>
application/views/second.php
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Tutorial::second</title>
</head>
<body>
Hello Second
</body>
</html>
뷰 로딩하기 (Loading a View)
application/controllers/Tutorial.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Tutorial extends CI_Controller {
public function index()
{
$this->load->view('index');
}
public function second()
{
$this->load->view('second');
}
}
확인
http://localhost/ci/Tutorial/index

http://localhost/ci/Tutorial/second

▶뷰에 데이터 전달
뷰에 전달하는 데이터는 배열 또는 객체로 전달하면 됩니다. (아래에 예제는 배열로 전달하겠습니다.)
컨트롤러 수정 (Controller)
application/controllers/Tutorial.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Tutorial extends CI_Controller {
public function index()
{
$data = array(
'title' => 'Tutorial::index',
'content' => 'Hello Index'
);
$this->load->view('index', $data);
}
public function second()
{
$data = array(
'title' => 'Tutorial::second',
'content' => 'Hello Second'
);
$this->load->view('second', $data);
}
}
뷰 수정 (View)
application/views/index.php
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><?php echo $title; ?></title>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
application/views/second.php
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><?php echo $title; ?></title>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
결과
실행해보면 결과는 이전과 같은 것을 확인할 수 있습니다.
▶여러 뷰 로딩하기 (Loading multiple views)
뷰 코드를 보면 index.php와 second.php 동일한 것을 확인할 수 있습니다.
이제 이 코드를 헤더(header)와 푸터(footer) 그리고 본문 뷰로 구분하여 호출하도록 하겠습니다.
뷰 생성 (Creating a View)
application/views/header.php
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><?php echo $title; ?></title>
</head>
<body>
application/views/content.php
<?php echo $content; ?>
application/views/footer.php
</body>
</html>
컨트롤러 수정 (Controller)
application/controllers/Tutorial.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Tutorial extends CI_Controller {
public function index()
{
$data = array(
'title' => 'Tutorial::index',
'content' => 'Hello Index'
);
$this->load->view('header', $data);
$this->load->view('content', $data);
$this->load->view('footer');
}
public function second()
{
$data = array(
'title' => 'Tutorial::second',
'content' => 'Hello Second'
);
$this->load->view('header', $data);
$this->load->view('content', $data);
$this->load->view('footer');
}
}
결과
실행해보면 결과는 이전과 같은 것을 확인할 수 있습니다.
▶뷰를 서브 디렉토리 안에 저장하기 (Storing Views within Sub-driectories)
뷰를 만들때 컨트롤러별로 뷰를 구분하고 싶을 떄는 서브 디렉토리를 만들어서 그 안에 뷰를 생성 후 호출하는 방법이 있습니다.
Tutorial 폴더 생성
application/views/Tutorial 폴더 생성
뷰 생성 (Creating a View)
기존에 Tutorial 컨트롤러에서 사용하는 뷰(Views)를 Tutorial 폴더로 이동.
- application/views/Tutorial/header.php
- application/views/Tutorial/content.php
- application/views/Tutorial/footer.php
컨트롤러 수정 (Controller)
application/controllers/Tutorial.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Tutorial extends CI_Controller {
public function index()
{
$data = array(
'title' => 'Tutorial::index',
'content' => 'Hello Index'
);
$this->load->view('Tutorial/header', $data);
$this->load->view('Tutorial/content', $data);
$this->load->view('Tutorial/footer', $data);
}
public function second()
{
$data = array(
'title' => 'Tutorial::second',
'content' => 'Hello Second'
);
$this->load->view('Tutorial/header', $data);
$this->load->view('Tutorial/content', $data);
$this->load->view('Tutorial/footer', $data);
}
}
결과
실행해보면 결과는 이전과 같은 것을 확인할 수 있습니다.
▶뷰를 데이터처럼 반환하기 (Storing Views within Sub-driectories)
뷰를 브라우저로 표시하지 않고, 데이터로 반환하는 방법이 존재합니다. 세 번째 파라미터에 기본 값은 FALSE입니다. 이 값을 TRUE로 설정하면 해당 뷰를 데이터로서 반환해줍니다.
$string = $this->load->view('welcome_message', '', true);
반응형