I may have a solution to manage custom uri in a CI application.
Let say there is a table named "routes"
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;
Then a custom "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);
}
}
Then a library I called 'route_manager' that creates / modifies a route and generate the custom route.php file (application/libraries/route_manager.php, creates application/cache/routes.php)
Note : Your app must be able to write files in 'application/cache'
<?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);
}
}
Then add a require_once (the 4th line in the example below) in your application/config/ routes.php file
<?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'] = '';
All will work using nice uri!
Exemple to create a custom fiendly uri, let's say 'super page that ROCKS! (really)' must be routed to "page" controller, "index" function with '23' as a parameter :
$this->load->library('route_manager');
$this->route_manager->set_alias('super page that ROCKS! (really)', 'page/index/23');
$this->route_manager->create_route_file();
Example to use that custom uri
echo site_url('page/index/23');
This example will generate a friendly and working url like this:
http://www.yourwebsite.com/index.php/super-page-that-rocks-really