먼저 URL 링크를 변경하여 url_title() (URL 도우미)으로 변경하십시오. 내가 데이터베이스 호출이라고 가정 한 ID의 텍스트 값을 사용하여 쿼리를 만들면
과 같은 결과를 얻을 수 있습니다 (예 : id 1 레코드가 name = "America"인 경우).
echo 'http://www.mysite.in/package/tour-packages/'.url_title($name);
줄 것입니다; 당신이 다음이 확장 된 라우터 클래스를 추가하는 경우
http://www.mysite.in/package/tour-packages/American
가 자동으로 밑줄로 대시를 다시 포맷합니다.
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
function set_class($class) {
$this->class = str_replace('-', '_', $class);
}
function set_method($method) {
$this->method = str_replace('-', '_', $method);
}
function _validate_request($segments) {
// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).EXT)) {
return $segments;
}
// Is the controller in a sub-folder?
if (is_dir(APPPATH.'controllers/'.$segments[0])) {
// Set the directory and remove it from the segment array
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
if (count($segments) > 0) {
// Does the requested controller exist in the sub-folder?
if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).EXT)) {
show_404($this->fetch_directory().$segments[0]);
}
} else {
$this->set_class($this->default_controller);
$this->set_method('index');
// Does the default controller exist in the sub-folder?
if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) {
$this->directory = '';
return array();
}
}
return $segments;
}
// Can't find the requested controller...
show_404($segments[0]);
}
}
그런 다음 경로 파일에 항목을 추가 할 수 있습니다 (설정). 같은 것;
$route['package/tour_package/(:any)'] = "package/lookup_by_name/$1";
당신은 다음 lookup_by_name ($ 이름)라는 패키지 컨트롤러의 방법을 만들어야합니다. 이 메서드는 이름 값에서 ID를 가져 오는 데이터베이스에 대해 SQL 쿼리를 수행해야합니다. 그런 다음 계속해서보기를로드하거나 원하는대로 수행 할 수 있습니다.
예컨대
public function lookup_by_id($name) {
// sql to get id from database record
// load the view?
$this->view($id);
}
public function view($id) {
// sql to load full record from id
$this->load->view('foo', $data);
}
하지만 새 URL로 리디렉션으로 변경하려면 또는 "미국"과 "아프리카"대신를 사용하려면 "1"과 "2"? – Patroklo