Welcome Guest, Not a member yet? Register   Sign In
How to accomplish this > www.mysite.com/username/controller/function/id
#5

[eluser]jadebot[/eluser]
@Psychonax

The problem with implementing your solution is that the site can only use one controller; the user controller. Once this controller is called, you can't run anything else.

---------------------------------------------------

So anyway, after testing out some stuff the whole day, I have finally got a working solution to my problem.

The problem in question is to run CI using this type of URI:
www.mysite.com/username/controller/method/id

This solution specifically addresses this problem.

Here's what I did:

Step 1: Remove index.php from your URI. Documentation for index.php removal

Step 2. Create a dynamic custom route. Routes Documention.

This is my specific instance:

Code:
/* Location: ./system/application/config/routes.php */

/* ------- Edits to enable www.mysite.com/username/controller/method/id  ---------  */
    
  //break URI into segments
    $parts = explode("/", $_SERVER['REQUEST_URI']);
    
  //delete any empty segments (allows for inclusion/exlusion of trailing slash)
    foreach ($parts as $key => $value) {
        if (is_null($value) || $value=="") {
            unset($parts[$key]);
        }
    }
    
    $site_name = $parts[1];

  //count number of useful segments being passed in URI
  //Subtract 1 from count since  $parts[1] is our username || For reference, $parts[0]  will always be deleted since it will always be empty
    $count = count($parts)-1;

  //build dynamic route. We need to execute at least once to catch and route all URI's
    do{
        $build_route .= ':any/';
        $count--;
    }
    while ($count > 0);
    $build_route = substr($build_route,0,-1);

  //build the URI to route to
    foreach($parts as $key => $part){
        if($key > 1){
            $build_url .= "{$part}/";
        }
    }
    $build_url = substr($build_url,0,-1);

  //generate finished route
    $route["$build_route"] = "$build_url";
    
  // *Troubleshoot*
    //print_r($parts);
    //echo '<br>$route["'.$build_route.'"] = "'.$build_url.'";';

Please note this is very raw code and I'm sure there is a more elegant way. I am fairly certain all this can be done with a couple lines of regular expressions but I know very little about regex so if anyone can improve this, I would appreciate it.

Step 3. Create a new hook. Hook Documentation

Code:
/* Location: ./system/application/config/hooks.php */
$hook['pre_system'] = array(
                                'class'    => '',
                                'function' => 'get_user_db',
                                'filename' => 'get_user_db.php',
                                'filepath' => 'hooks',
                                );

And here is the function I'm calling ---

Code:
/* Location: ./system/application/hooks/get_user_db.php */

function get_user_db()
{
    // get user database configuration from a master database you set up to store user credentials
    
    $host = "";
    $user = "user";
    $pass = "pass";
    $db = "database";

    $parts = explode("/", $_SERVER['REQUEST_URI']);
    $site_name = $parts[1];
    
    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
    mysql_select_db($db) or die ("Unable to select master database!");

    $query = " SELECT * FROM users_db WHERE site='{$site_name}'";
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    if (mysql_num_rows($result) > 0) {
        while($row = mysql_fetch_array($result)) {        
            $username = $row['username'];
            $password = $row['password'];
            $database = $row['database'];        
        }
    }
    else{
        /*
          if no records found for user you must specify default user
          redirect to home will not work because this function will get called again and run a never ending loop
          You also can not specify a default path in config other than root because CI will append your default path to every
          link your generate. This will mess up the www.site.com/username/controller/method/id  configuration.
        */
        header('Location: /jade');
        die();
    }        
    mysql_close();
        
    /* please see notes following to explain what im doing here ... */    
    // Grab the default file
    $file_path = APPPATH.'extras/base_db.php';
    $file = file_get_contents($file_path);

    // Change the values
    $file = str_replace(array('__DB_USERNAME__', '__DB_PASSWORD__','__DB_DATABASE__'), array($username, $password, $database), $file);

    // Put it in the proper spot
    $config_path = APPPATH.'config/database.php';
    file_put_contents($config_path, $file);

    /* ------------------------ we are now working in user-specific database -------------------- */
}

The file rewrite at the bottom was taken from member: inparo's advice found here. This works on my dev platform, but since it continually rewrites the same file, I wonder ifit works in a multi-user production environment? Help on this would also be appreciated!

These steps will see to it that the user database of your choice is dynamically loaded based on the "username" segment in your URLS.

I should note that using this will force your application to always require a username. So you have to run your platform from within a users database as well.
In other words there will be no more www.mysite.com , instead you will need to run off of www.mysite.com/mysite where your the username "mysite" is an instance of your application that happens to run the root application.

Hopefully that makes sense! Please advise! Thanks.


Messages In This Thread
How to accomplish this > www.mysite.com/username/controller/function/id - by El Forum - 11-05-2008, 06:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB