Welcome Guest, Not a member yet? Register   Sign In
Help and suggestions for making a short URL structure?
#4

(This post was last modified: 12-02-2017, 01:29 PM by PaulD. Edit Reason: Added PS )

I was thinking about a pre-system hook to change the routing. So I could look up the first segment, and somehow reset the controller called if necessary (not exactly certain how yet, but been looking through codeigniter.php and index.php for ideas).

So armed with this new idea I found this: https://stackoverflow.com/questions/2885...on-best-ap

That suggests something like the following for a pre-system hook. (just tidied up the comments a bit, not changed anything else yet)
Code:
function set_controller()
{    
    // Gather the DB connection settings
    include_once APPPATH.'config/database.php';
    
    // Connect to the DB server
    $link = mysql_connect($db[$active_group]['hostname'], $db[$active_group]['username'], $db[$active_group]['password']) or die('Could not connect to server.' );
    
    // Select the DB
    mysql_select_db($db[$active_group]['database'], $link) or die('Could not select database.');
    
    // Break apart the URL variable
    $URI = explode('/',key($_GET));
    
    // Query the DB with the URI segment
    $query = 'SELECT * FROM theDomainTable WHERE domainName = "'.$URI[1].'"';
    
    // Only deal with controller requests that exist in the database
    if($results = mysql_fetch_array(mysql_query($query))){
    
        // Replace the controller segment
        $URI[1] = $results['controllerName'];
        
        // Reconstruct and replace the GET variable
        $_GET = array(implode('/',$URI)=>NULL);
    }
    
    // Close the DB link
    mysql_close($link);
}

That looks like it has potential as a way to go.

I also had a look at the routing where it throws an error if the controller is not found. I was tempted to do the lookups there and tamper with it but in truth, there are things it is doing in the php there I do not fully understand. (And tampering with these files is always a bad idea)

I might give the above code a go, see if I can modify it to do what I need. Still causes me concern though as this is rapidly getting beyond my working knowledge of CI, and I do not want to cause massive issues further down the line with unforeseen side affects.

Paul.

PS This doesn't seem to be working. At least I have been unable to get this idea to work :-(
Reply


Messages In This Thread
RE: Help and suggestions for making a short URL structure? - by PaulD - 12-02-2017, 08:16 AM



Theme © iAndrew 2016 - Forum software by © MyBB