Codeigniter RESTful URL
codeigniter 의 괜찮은 확장라이브러리를 찾아 소개한다.
주소체계가 아래와 같은경우
http://www.example.com/controller_name/method_name/query1/value1/query2/value2
다음처럼 손쉽게 추출이 가능합니다.
$query1 = $this->input->rest('query1′); // returns "value1″ in this example
$query2 = $this->input->rest('query2′); // returns "value2″
The Library
segment_array(); $url = current_url(); } /* Only pay attention after the method */ $arrUri = array_slice($arrUri, array_search($objRouter->method, $arrUri)); $queryId = array_search($index, $arrUri); if($queryId !== false) { /* The next one along is the REST value */ $valueId = $queryId + 1; if(array_key_exists($valueId, $arrUri)) { $value = urldecode($arrUri[$valueId]); /* Do we have to clean out the filth */ if($xss_clean) { $value = $this->xss_clean($value); } return $value; } } return false; } /** * REST Replace * * Replaces the REST value in the URL. Takes the * old value and puts the new value in there. You * can specify the URL (useful if it's coming via * an AJAX request) or base it on the current_url() * * @param string $index * @param string $value * @param string $url * @return string */ function rest_replace($index, $value, $url = false) { $objRouter = &load;_class('Router'); $objUri = &load;_class('URI'); /* Are we checking the current_url or specified one? */ if($url !== false) { /* Specified - extract the domain */ $url = preg_replace('/^'.preg_quote(base_url(), '/').'/', '', $url); $arrUri = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $url)); /* Add the domain back in */ $url = site_url($url); } else { /* Current */ $arrUri = $objUri->segment_array(); $url = current_url(); } $original = $this->rest($index, false, $url); if($original !== false) { /* Something's already set */ $queryId = array_search($index, $arrUri); unset($arrUri[$queryId]); unset($arrUri[$queryId + 1]); $url = site_url(implode('/', $arrUri)); } /* We've got the URL less the REST key - only set if not false or null */ if(!is_null($value) && $value !== false) { $url .= '/'.$index.'/'; $url .= urlencode($value); } return $url; } }
출처 : http://www.simonemms.com/code/codeigniter-restful-url/
from http://chess72.tistory.com/136 by ccl(A) rewrite - 2020-03-06 07:54:42
댓글
댓글 쓰기