我可能有一个解决方案来管理 CI 应用程序中的自定义 uri。
假设有一个名为“路由”的表
CREATE TABLE IF NOT EXISTS `routes` (
`alias` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`uri` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`alias`),
UNIQUE(`alias`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
然后是一个自定义的“url helper”(application/helper/MY_url_helper.php)
// function that produces a safe uri
if( ! function_exists('safe_uri'))
{
function safe_uri($str, $replace=array(), $delimiter='-') {
if( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
}
// overriden function that checks if the uri exists in the routing table. If so, use this uri instead
if ( ! function_exists('site_url'))
{
function site_url($uri = '')
{
$CI =& get_instance();
if(in_array($uri, $CI->router->routes)){
$key = array_search($uri, $CI->router->routes);
if($key)$uri=$key;
}
return $CI->config->site_url($uri);
}
}
然后是我称为“route_manager”的库,它创建/修改路由并生成自定义 route.php 文件(application/libraries/route_manager.php,创建 application/cache/routes.php)
注意:您的应用程序必须能够在“应用程序/缓存”中写入文件
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class route_manager{
public function set_alias($full_text, $uri){
$this->load->helper('url');
$sql = 'INSERT INTO `routes` (`alias`, `uri`) VALUES(?,?)
ON DUPLICATE KEY UPDATE
uri=VALUES(uri)';
$this->db->query($sql, array(safe_uri($full_text), $uri));
}
public function create_routes_file(){
$res = $this->db->get('routes');
$output = '<' . '?' . 'php ' . 'if ( ! defined(\'BASEPATH\')) exit(\'No direct script access allowed\');' . CRLF . CRLF;
foreach($res->result_array() as $rs){
$output .= '$' . 'route[\'' . $rs['alias'] . '\'] = "' . $rs['uri'] . '";'. CRLF;
}
// unsure the file won't generate errors
$route = array();
eval('?> '.$output);
// if error detected, the script will stop here (and won't bug the entire CI app). Otherwize it will generate the cache/route.php file
$this->load->helper('file');
write_file(APPPATH . 'cache/routes.php', $output);
}
}
然后在 application/config/routes.php 文件中添加一个 require_once(下例中的第 4 行)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// require custom routes (if the file exists)
if(file_exists(APPPATH . 'cache/routes.php'))require_once(APPPATH . 'cache/routes.php');
$route['default_controller'] = "welcome";
$route['404_override'] = '';
一切都将使用漂亮的uri!
例如,创建一个自定义的 uri,假设是“超级页面,ROCKS!(真的)' 必须路由到“页面”控制器,“索引”函数,参数为“23”:
$this->load->library('route_manager');
$this->route_manager->set_alias('super page that ROCKS! (really)', 'page/index/23');
$this->route_manager->create_route_file();
使用该自定义 uri 的示例
echo site_url('page/index/23');
此示例将生成一个友好且有效的 url,如下所示:
http://www.yourwebsite.com/index.php/super-page-that-rocks-really