Php codeigniter中的漂亮url设置
我有一个带有控制器“package”、函数“tour_package”和参数“1”的url。 这里的参数被设置为id。我需要用相应的url字符串来更改它 另一个例子是: 到Php codeigniter中的漂亮url设置,php,codeigniter,Php,Codeigniter,我有一个带有控制器“package”、函数“tour_package”和参数“1”的url。 这里的参数被设置为id。我需要用相应的url字符串来更改它 另一个例子是: 到 如何做到这一点?首先,我会将url链接更改为使用url\u title()(url帮助器)。如果您使用ID的文本值创建查询,我认为这是一个数据库调用,这将为您提供如下信息 (例如,如果id 1的记录为name=“America”) 愿意付出 http://www.mysite.in/package/tour-packag
如何做到这一点?首先,我会将url链接更改为使用url\u title()(url帮助器)。如果您使用ID的文本值创建查询,我认为这是一个数据库调用,这将为您提供如下信息 (例如,如果id 1的记录为name=“America”) 愿意付出
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]);
}
}
然后需要在包控制器中创建一个名为按名称($name)查找的方法。
此方法需要对数据库执行sql查询,从中可以从名称值获取id。然后,您可以继续加载视图或执行任何您想要的操作
例如
但是你想通过重定向到新的url来更改它,或者你想使用“America”和“Africa”而不是“1”和“2”?既然它似乎只是简单地用名称替换ID,为什么不在数据库中添加一列“slug”并将其设置为
url\u title($name)
。从那以后,只需选择slug而不是ID@danneth您的建议是有效的(而且很好),但用于选择记录的内容并不重要,我的回答只是显示了一种建议的使用自定义路由进行查找的方法。它实际上看起来相当整洁,我自己从来没有想过。我不是问这个问题的人:)@danneth,对不起,我分心了。
<?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";
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);
}