Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] www.mysite.com/username/method/.. Impossible?
#1

[eluser]markanderson993[/eluser]
Hello CodeIgniter gods! I was wondering if it would be possible to get Codeigniter to recognize if the first uri segment (the controller) is a username and do the following:

Traditional Uri Schema
www.mysite.com/controller/method/..

Changed to this to..
www.mysite.com/username/method/.. ?

The latter option would require Codeigniter to check the controller request each time against a database full of users to determine if a username match exists. If not, just load in the traditional uri.

Is this possible? If so, how might I achieve this?!

Any help is greatly appreciated! Thank you very much in advance,
Mark Anderson
#2

[eluser]SpooF[/eluser]
You can do something like this: Place this in your routes.
Code:
$controllers = array();

if($controller_dir = opendir(APPPATH . 'controllers'))
{
    while(false !== ($file = readdir($controller_dir)))
    {
        if(preg_match('/(.*?)\.php$/i',$file,$file))
        {
            $controllers [] = $file[1];
        }
    }
}
else
{
    log_message('error','Could not read controllers directory at |' . APPPATH . 'controllers');    
}

foreach($controllers as $controller)
{
    $route[$controller . '(.*?)'] = $controller . "$1";
}

$route['(.*?)/(.*?)'] = "users/$2/$1";

This will create a route for all your controllers (stops the last one from matching everything), if the controller doesnt exists it will default to the users controller.

If you separate your controllers into groups via folders you will have to modify the script a little.
#3

[eluser]Jake Grice[/eluser]
You might also be able to use mod_rewrite and distinguish mysite.com/sometext/othertext from mysite.com/index.php/controller
#4

[eluser]markanderson993[/eluser]
Thank you very much for your suggestions! I appreciate the help very much!
Mark Anderson




Theme © iAndrew 2016 - Forum software by © MyBB