Custom URL handling (just a little)

#1
[eluser]Jupiter[/eluser]
I'd like to parse URLs before CI dows routing. Purpose is take the first level (the controller) out of the URL and use the segments as a tree-like structure to display contents of my database.

Example:

www.mysite.com/shoes/sneakers/nike

should display all rows from my db where
category = 'shoes', type = 'sneakers' and brand = 'nike'.

I cannot setup controllers for every category (because they keep adding up) and I don't want to generally disable the URL-handling. Actually I just want to check, if there is a category matching the first segment and handle it (and in case there isn't - continue with CI URLs as usual).

Any hint where to begin?


Peter

#2
[eluser]GSV Sleeper Service[/eluser]
just use the URL segments

say for example you have a default controller called Store

Code:
class Store extends Controller {

    function index($category = false, $type = false, $brand = false) {
        //obviously only run this if $category, $type and $brand have been set
        $q = "SELECT * FROM store WHERE category = '$category' AND type = '$type' AND brand = '$brand'";
    }

}

[edit] see here for more details - http://ellislab.com/codeigniter/user-gui...s/uri.html

#3
[eluser]xwero[/eluser]
The GSV Sleeper Service is for one controller only and it has to be the default controller. If your application matches those requirements it's the solution for you.

If one of the requirements is not met i think you are better of using routing instead of trying to create a custom routing.

#4
[eluser]louis w[/eluser]
I would create a custom loader.php file (My_Loader.php in app libraries). Currently it kicks to a 404 page at the end if no controller is found. Instead check if there is a uri set and send it to a specified controller. Then use the uri->segments thing to pull out the data.

#5
[eluser]Jupiter[/eluser]
Thanks a lot for the quick replies!

I think, what louis wrote is actually what I was looking for but a custom function in routing.php might also do it. When I have found a solution, I'll post it here!

Peter

#6
[eluser]Jupiter[/eluser]
Now here's what I did:

In routes.php I added

Code:
// CUSTOM ROUTES:

// database is not yet connected, so do it manually:

$dbhost = 'localhost';
$dbuser = 'username';
$dbpassword = 'password';
$dbh = @mysql_connect($dbhost, $dbuser, $dbpassword);

$sql = "SELECT brand_name FROM brand";

$result = mysql_query($sql);

// insert route for every brand here:

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    
    $route[$brand] = "display/brand/".$row["brand_name"];
    $route[$brand.'/:any'] = "display/brand/".$row["brand_name"];

    }

That's not beautiful, but it works. Any ideas?

Peter

#7
[eluser]louis w[/eluser]
You should DEF not be doing that mysql_connect there. You realize that CI has an entire database library to centralize everything? Pass this off to a controller and let it do all the work. Not to mention loading and parsing all the brands every single page request is not a good idea performance wise.

#8
[eluser]Jupiter[/eluser]
Louis,

when processing routes.php , CI has not yet connected to the database, so I have to do it manually - or is there any way to load the db libraries before routing takes place?

Where can I put my custom routes so that it will be skipped, when a contoller or another routing fits the URL?

Peter

#9
[eluser]louis w[/eluser]
Thats why I would be doing this in loader.php

Right before it currently sends to a 404 (at the end), query the db with just the brand in the uri (don't get all of them). If its valid then send to your controller.

#10
[eluser]Jupiter[/eluser]
Are you refering to the Loader.php library? Where exactly does it send to 404?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.