URI Class

URI 클래스는 URI 문자열로부터 원하는 정보를 추출할 수 있도록 해주는 함수를 제공합니다. 만약 URI 라우팅을 사용하신다면, 재 라우팅(re-routed ) 새그먼트에 대한 정보도 추출할 수 있습니다.

Note

클래스는 시스템에의해 자동으로 로드되므로 직접 로드하실 필요가 없습니다.

클래스 레퍼런스 Class Reference

class CI_URI
segment($n[, $no_result = NULL])
인수:
  • $n (int) – Segment index number
  • $no_result (mixed) – What to return if the searched segment is not found
반환값:

Segment value or $no_result value if not found

반환형:

mixed

특정 새그먼트를 추출해 냅니다. n 은 추출하고자 하는 새그먼트의 번호입니다. 새그먼트번호는 왼쪽에서 오른쪽으로 매겨집니다. 예를 들어, URL 이 다음과 같다면:

http://example.com/index.php/news/local/metro/crime_is_up

새그먼트번호는 아래와 같습니다:

  1. news
  2. local
  3. metro
  4. crime_is_up

기본값으로, 새그먼트가 존재하지 않으면 FALSE를 리턴합니다. 필요하다면, 두 번째 파라미터로 새그먼트가 없을 때 리턴될 기본값을 지정할 수 있습니다. 예를 들어, 아래 예제는 실패시에 숫자 0을 리턴하도록 합니다:

$product_id = $this->uri->segment(3, 0);

위 코드는 아래와 같은 코드를 작성할 필요가 없도록 해줍니다:

if ($this->uri->segment(3) === FALSE)
{
        $product_id = 0;
}
else
{
        $product_id = $this->uri->segment(3);
}
rsegment($n[, $no_result = NULL])
인수:
  • $n (int) – Segment index number
  • $no_result (mixed) – What to return if the searched segment is not found
반환값:

Routed segment value or $no_result value if not found

반환형:

mixed

이 함수는 위의 함수 segment(), 와 동일합니다. 다만, CodeIgniter URI 라우팅을 사용하실 때 최종적으로 재 라우팅(re-routed) 되는 URI 의 특정 새그먼트의 값을 추출한다는 것만 다릅니다.

slash_segment($n[, $where = 'trailing'])
인수:
  • $n (int) – Segment index number
  • $where (string) – Where to add the slash (‘trailing’ or ‘leading’)
반환값:

Segment value, prepended/suffixed with a forward slash, or a slash if not found

반환형:

문자열

이 함수는 segment() 와 거의 같습니다. 다만, 두 번째 파라미터에 의해 앞(leading), 뒤(trailing) 혹은 양쪽(both)에 슬래시(/)를 추가한다는 점만 다릅니다. 파라미터를 사용하지 않으면 뒤쪽 슬래시가 붙습니다. 예제:

$this->uri->slash_segment(3);
$this->uri->slash_segment(3, 'leading');
$this->uri->slash_segment(3, 'both');

반환값:

  1. segment/
  2. /segment
  3. /segment/
slash_rsegment($n[, $where = 'trailing'])
인수:
  • $n (int) – Segment index number
  • $where (string) – Where to add the slash (‘trailing’ or ‘leading’)
반환값:

Routed segment value, prepended/suffixed with a forward slash, or a slash if not found

반환형:

문자열

이 함수는 위의 함수 slash_segment() 와 동일합니다. 다만, CodeIgniter URI 라우팅 을 사용하실 때 최종적으로 재 라우팅(re-routed) 되는 URI 의 특정 새그먼트에 슬래시(/)를 추가한다는점만 다릅니다.

uri_to_assoc([$n = 3[, $default = array()]])
인수:
  • $n (int) – Segment index number
  • $default (array) – Default values
반환값:

Associative URI segments array

반환형:

array

이 함수는 URI 새그먼트를 키/값 쌍으로된 연관배열로 리턴합니다. 아래 URI를 보시죠:

index.php/user/search/name/joe/location/UK/gender/male

이 함수를 사용하시면 아래와 같은 형태의 연관배열을 얻을 수 있습니다:

[array]
(
        'name'          => 'joe'
        'location'      => 'UK'
        'gender'        => 'male'
)

첫 번째 파라미터는 오프셋을 설정할 수 있게 합니다. 기본값은 3인데 왜냐하면 URI는 일반적으로 컨트롤러/함수 를 첫 번째및 두 번째 새그먼트로 가지기 때문입니다. 예제:

$array = $this->uri->uri_to_assoc(3);
echo $array['name'];

두 번째 파라미터로 기본 키 이름을 설정할 수 있습니다. 그렇게하면 URI에 해당하는값이 없더라도, 언제나 예상되는 인덱스를 포함한 배열을 리턴합니다. 예제:

$default = array('name', 'gender', 'location', 'type', 'sort');
$array = $this->uri->uri_to_assoc(3, $default);

만약 URI 가 여러분의 default에 있는 값을 포함하지 않더라도, 배열인덱스는 지정한 이름으로 설정되며 값은 FALSE 로 설정됩니다.

마지막으로, 주어진 키에 대응되는 값을 발견할 수 없을 때 (URI 새그먼트가 홀수라든지) 값은 FALSE 로 설정됩니다.

ruri_to_assoc([$n = 3[, $default = array()]])
인수:
  • $n (int) – Segment index number
  • $default (array) – Default values
반환값:

Associative routed URI segments array

반환형:

array

이 함수는 위의 함수 uri_to_assoc() 와 동일합니다. 다만, CodeIgniter URI 라우팅을 사용하실 때 최종적으로 재 라우팅(re-routed) 되는 URI 로부터 연관배열을 생성한다는점만 다릅니다.

assoc_to_uri($array)
인수:
  • $array (array) – Input array of key/value pairs
반환값:

URI string

반환형:

문자열

연관배열을 입력받아 URI 문자열을 생성합니다. 배열키도 문자열에 포함됩니다. 예제:

$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');
$str = $this->uri->assoc_to_uri($array);

// Produces: product/shoes/size/large/color/red
uri_string()
반환값:URI string
반환형:string

완전한 URI 문자열을 리턴합니다. 예를 들어, 전체 URL이 아래와 같다면:

http://example.com/index.php/news/local/345

함수는 아래를 리턴합니다:

news/local/345
ruri_string()
반환값:Routed URI string
반환형:string

이 함수는 위의 함수 uri_string() 와 완전히 동일합니다. 다만, CodeIgniter URI Routing을 사용하실 때 최종적으로 재 라우팅(re-routed) 되는 URI 를 리턴한다는 점만 다릅니다.

total_segments()
반환값:Count of URI segments
반환형:int

새그먼트 전체 개수를 리턴합니다.

total_rsegments()
반환값:Count of routed URI segments
반환형:int

이 함수는 위의 함수 total_segments()와 완전히 동일합니다. 다만, CodeIgniter URI 라우팅을 사용하실 때 최종적으로 재 라우팅(re-routed) 되는 URI 새그먼트의 전체개수를 리턴한다는 점만 다릅니다.

segment_array()
반환값:URI segments array
반환형:배열(array)

URI 새그먼트를 포함한 배열을 리턴합니다. 예제:

$segs = $this->uri->segment_array();

foreach ($segs as $segment)
{
        echo $segment;
        echo '<br />';
}
rsegment_array()
반환값:Routed URI segments array
반환형:배열(array)

이 함수는 위의 함수 segment_array() 와 완전히 동일합니다. 다만, CodeIgniter URI 라우팅을 사용하실 때 최종적으로 재 라우팅(re-routed) 되는 URI 새그먼트를 포함한 배열을 리턴한다는 점만 다릅니다.