쿼리스트링 가지고 놀기

inhalin·2021년 7월 4일
0

일터지식

목록 보기
7/14

url_qs 시리즈

url_qs_only

특정 쿼리 스트링만 포함한 주소 가져오기

  • $onlyKeys에 써준 쿼리 스트링만 가져오고 나머지는 다 지워준다.
  • $setKeys$path에 없는 쿼리 스트링을 추가하거나 $onlyKeys에 있는 쿼리 스트링에 특정값을 넣어줄 때 사용한다.
/**
 * `$path` 주소 필수 입력. 가능한 url($path) 형태의 입력 권장.
 * `$onlyKeys` 쿼리 스트링에서 가져올 키들.  미 입력시 모든 쿼리 스트링 사용.
 * `$setKeys` 새로운 키값 추가 또는 기존 qs 덮어쓰기
 */
function url_qs_only($path, $onlyKeys = [], $setKeys = [])

예시

// 현재 url - htts://test.com/dev?field=all&q=hello&type=company&location=seoul

url_qs_only();
// ArgumentCountError Too few arguments ...

url_qs_only(url($path));
// dev?field=all&q=hello&type=company&location=seoul

url_qs_only('qna', ['field', 'q']);
// qna?field=all&q=hello

url_qs_only('carrot', ['location'], ['is_used' => '1', 'location' => 'siheung']);
// carrot?location=siheung&is_used=1

url_qs_only('user', ['type']);
// user?type=company

url_qs_except

특정 쿼리 스트링만 제외한 주소 가져오기

  • $exceptKeys에 써준 쿼리 스트링은 지워버리고 나머지는 그대로 남겨놓는다.
  • url_qs_except$setKeysurl_qs_only와 사용법이 같다.
/**
 * `$path` 주소 필수 입력. 가능한 url($path) 형태의 입력 권장.
 * `$exceptKeys` 쿼리 스트링에서 제외할 키들.  미 입력시 모든 쿼리 스트링 사용.
 * `$setKeys` 새로운 키값 추가 또는 기존 qs 덮어쓰기
 */
function url_qs_except($path, $exceptKeys = [], $setKeys = [])

예시

// 현재 url - htts://test.com/dev?field=all&q=hello&type=company&location=seoul

url_qs_except(url($path));
// dev?field=all&q=hello&type=company&location=seoul

url_qs_except(url($path), ['field', 'q', 'location']);
// dev?type=company

url_qs_except(url($path), ['is_used'], ['type' => 'premium']);
// dev?field=all&q=hello&type=premium&location=seoul

current_url_qs 시리즈

기본적으로 url_qs 시리즈와 매우 비슷한데 무조건 현재 페이지의 주소에 대한 쿼리스트링을 가지고 노는 거라서 $path를 지정하지 않는다.

current_url_qs_only

function current_url_qs_only($onlyKeys = [], $setKeys = [])

예시

// 현재 url - htts://test.com/dev?field=all&q=hello&location=seoul

current_url_qs_only(['location', 'isUsed'], ['type' => 'person']);
// /dev?location=seoul&type=person

current_url_qs_except

function current_url_qs_except($onlyKeys = [], $setKeys = [])

예시

// 현재 url - htts://test.com/dev?field=all&q=hello&location=seoul

current_url_qs_except(['field', 'q']);
// /dev?field=all&q=hello

form의 queryString 시리즈

form()->queryStringOnly()

지정해준 쿼리스트링의 값만 input hidden으로 값 넘겨주기

form()->queryStringExcept()

지정한 쿼리스트링을 제외하고 input hidden으로 값 넘겨주기

0개의 댓글