CI Helper
Posted by Albert 4087Day 2Hour 56Min 29Sec ago [2014-02-11]
Helper
헬퍼란 자주 사용하는 로직을 재활용 할 수 있게 만든 일종의 Library다. CI에는 라이브러리라는 개념이 별도로 존재하는데 Helper와 Library의 차이점은 Helper가 객체지향이 아닌 독립된 함수라면 Libary는 객체지향인 클래스다.
Helper 사용
CI에서 기본적으로 제공하는 Helper는 아래와 같다.
- 배열(Array)
- CAPTCHA 헬퍼
- 쿠키(Cookie)
- 날짜(Date)
- 디렉토리(Directory)
- 다운로드(Download)
- 이메일(Email)
- 파일(File)
- 폼(Form)
- HTML
- 인플렉터(어형변화)
- 언어(Language)
- 숫자(Number)
- 경로(Path)
- 보안(Security)
- 스마일리(Smiley)
- 문자열(String)
- 텍스트처리(Text)
- 타이포그라피(Typography)
- URL
- XML
핼퍼를 사용하기 위해서는 사용하고자 하는 Helper를 로드해야 한다. 핼퍼를 로드할 때는 아래와 같은 방법을 사용한다.
1 | $this ->load->helper( '핼퍼의 이름' ); |
복수의 핼퍼를 로드하기 위해서는 아래와 같이 한다.
1 | $this ->load->helper( array ( '핼퍼1의 이름' , '핼퍼2의 이름' )); |
또는 application/config/autoload.php의 $autoload helper 값으로 핼퍼 리스트가 담긴 배열을 전달하면 된다.
1 | $autoload [ 'helper' ] = array ( 'url' , 'file' ); |
예를들어 URL과 관련된 Helper를 로드하려면 아래와 같이 한다.
1 | $this ->load->helper( 'url' ); |
그리고 Controller,View,Model에서 url 핼퍼와 관련된 함수를 호출하면 된다.
Helper 제작
Helper는 가볍게 만들기 좋은 라이브러리다. 만드는 방법은 아래와 같다.
- application/helper 디렉토리에
- date_helper.php 파일을 만들고
- 그 속에 korean_date()를 정의한다.
태그
태그명 : Helper
태그주소 : https://github.com/egoing/codeigniter_codeingeverbody/tree/Helper
예제
application/controllers/topic.php
차이점
코드
1234567891011121314151617181920212223242526 if
( ! defined(
'BASEPATH'
))
exit
(
'No direct script access allowed'
);
class
Topic
extends
CI_Controller {
function
__construct()
{
parent::__construct();
$this
->load->database();
$this
->load->model(
'topic_model'
);
}
function
index(){
$this
->load->view(
'head'
);
$topics
=
$this
->topic_model->gets();
$this
->load->view(
'topic_list'
,
array
(
'topics'
=>
$topics
));
$this
->load->view(
'main'
);
$this
->load->view(
'footer'
);
}
function
get(
$id
){
$this
->load->view(
'head'
);
$topics
=
$this
->topic_model->gets();
$this
->load->view(
'topic_list'
,
array
(
'topics'
=>
$topics
));
$topic
=
$this
->topic_model->get(
$id
);
$this
->load->helper(
array
(
'url'
,
'HTML'
,
'korean'
));
$this
->load->view(
'get'
,
array
(
'topic'
=>
$topic
));
$this
->load->view(
'footer'
);
}
}
?>
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 |
|
application/helper/korean_helper.php
1234567 if
( ! defined(
'BASEPATH'
))
exit
(
'No direct script access allowed'
);
if
( ! function_exists(
'kdate'
)){
function
kdate(
$stamp
){
return
date
(
'o년 n월 j일, G시 i분 s초'
,
$stamp
);
}
}
1 2 3 4 5 6 7 |
|
application/models/topic_model.php
차이점
코드
1234567891011121314151617181920
class
Topic_model
extends
CI_Model {
function
__construct()
{
parent::__construct();
}
function
gets(){
return
$this
->db->query(
"SELECT * FROM topic"
)->result();
}
function
get(
$topic_id
){
$this
->db->select(
'id'
);
$this
->db->select(
'title'
);
$this
->db->select(
'description'
);
$this
->db->select(
'UNIX_TIMESTAMP(created) AS created'
);
return
$this
->db->get_where(
'topic'
,
array
(
'id'
=>
$topic_id
))->row();
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
application/view/get.php
차이점