,但我有一個擴展的路由器類,將想要你想要的。
它將解析任何具有' - '的控制器方法和變量並將其替換爲'_'。
你可以這樣做;
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