Extend the Router Class by placing a MY_Router.php
in your application\libraries
directory and use this code:
<?php
class MY_Router extends CI_Router {
function _validate_request($segments)
{
// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.$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().$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;
}
// **
// THIS IS THE NEW CODE BELOW
// **
// It forces the segments to your known class (user) & method (index)
// for all controller calls that don't exist as files or inside
// directories
$my_segments = array('user', 'index', $segments[0]);
return $my_segments;
}
}
Now, just create a User
controller with an index method that accepts username as the first parameter:
<?php
class User extends Controller {
function index($username = '')
{
// Validate the HECK out of $username
// Validate the HECK out of $username
// VALIDATE THE HECK OUT OF $username
echo $username;
exit();
}
}
That's a ballin' answer! Tested on CI 1.7.2. Don't know about 2.0, though...