,但我有一个扩展的路由器类,将想要你想要的。
它将解析任何具有' - '的控制器方法和变量并将其替换为'_'。
你可以这样做;
public function item($name)
{
$this->data['item'] = $this->db->get_where('items', array('name' => str_replace("_", " ", $name)))->result_array();
$this->layouts->view('databases/items/single', $this->data);
}
类(MY_Router.php)需要被放在应用/核心(介意类扩展)。
<?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]);
}
}
这是使用像lucene这样的搜索引擎更容易完成的事情,而不是db – 2012-04-03 12:36:30
codeigniter的get_where部分,我会如何放置数组('name'=> $ name)($ name field thing ) – unlucky4ever 2012-04-03 12:37:02
您必须将“slug”存储在数据库中。将'davids-print'''去掉''David's Print“'很难。编辑:是“不插电”是一个字。 – vstm 2012-04-03 12:40:45