Welcome Guest, Not a member yet? Register   Sign In
URL Routing Filtering
#1

[eluser]koichirose[/eluser]
Hi, I'm trying to filter a url_segment from my URL, just like this gem for Rails does:
https://github.com/svenfuchs/routing-filter

My setup:
website at market.website.com
I need to create a new section: market.website.com/luxury

every controller behaves pretty much as if /luxury weren't there.
/luxury is used to set a variable and modify queries (i.e. get all products in the luxury category only) and css classes to change the layout of the website (this is not a problem and I'm doing it already).
I also edit my base url, so I can carry my 'luxury' segment around.
Code:
if (stristr($uri_string, 'luxury')) {
   $this->config->set_item('base_url', $this->config->item('base_url').'luxury');
  }

My issue is with urls: I need to setup new routes so that 'luxury/cart' goes to 'mkt/cart', just like 'cart'. The cart controller is inside the 'mkt' directory, to make matters worse.

some of my routes:
Code:
$route['banner_landing/(:any)'] = "banner_landing/$1";

if (!$is_mkt) { //enters here if url is www.website.com
$route['stores_list'] = "public/stores_list";
        //other routes
}
else { //enters here if url is market.website.com
$route['default_controller'] = "mkt/home";

$route['stores_list'] = "mkt/stores";
$route['stores/read_reviews/(:num)'] = "mkt/stores/read_reviews/$1";
$route['stores/:any/(:num)'] = "mkt/stores/single_store/$1";

$route['cart/(:any)'] = "mkt/cart/$1";

$route['login'] = "mkt/login/index";
//other routes

$route['luxury'] = "mkt/home";
        $route['luxury/(:any)'] = "mkt/$1";

$route['(:any)'] = "mkt/$1";
}

This is not working.
It works if I duplicate all my existing routes by prepending 'luxury', which I don't think is the best way and it's also very error prone and not 100% safe, I think.

What can I do?

Sorry for the lenghty post.
#2

[eluser]TheFuzzy0ne[/eluser]
Sorry. You lost me at "Luxury".

Please could you give a few examples of URLs and what they should map to?

Rather than duplicating your routes by hand, why not keep them in an array, and loop through them, and prepend "luxury" to the beginning of the route before throwing it into the $route array?

Sorry for being dense. I keep meaning to check out RoR, but never have.
#3

[eluser]koichirose[/eluser]
Basically, I need everything inside the else block (thus, when I'm at market.website.com) to work with or without 'luxury'. This needs to work with or without parameters (either URL segments or GET parameters, which I enabled).

So:
market.website.com/cart goes to mkt/cart
market.website.com/luxury/cart goes to mkt/cart
and so on.

I've thought about cycling over existing routes and sort of duplicating them, but I hoped there would be something more elegant, like (I don't know what I'm talking about) extending the routing class and removing 'luxury' internally so I don't have to touch routes and hopefully everything would be more robust.

#4

[eluser]TheFuzzy0ne[/eluser]
It's an interesting problem, and extending the Router class might be a good idea. It would certainly save adding a load of routes, since you do have a predictable pattern.

I wonder if this could work:
Code:
$route['banner_landing/(:any)'] = "banner_landing/$1";

if (!$is_mkt) { //enters here if url is www.website.com
    $route['stores_list'] = "public/stores_list";
    //other routes
}
else { //enters here if url is market.website.com
    $route['default_controller'] = "mkt/home";

    // Not quite sure how to handle this one. It depends on the format of the URI.
    $route['(luxury/)?stores/:any/(:num)'] = "mkt/stores/single_store/$2";
    
    // This route might not even be necessary.
    $route['luxury'] = "mkt/home";
    
    // If you change the stores() method to stores_list(), you can drop this route.
    $route['(luxury/)?stores_list'] = "mkt/stores";
    $route['(luxury/)?(.+)?'] = "mkt/$2";
}

I've added some comments there too, but this might be a bit more robust. These routes essentially strip out "luxury" from the beginning of the URI. However, the less custom routes you have, the more robust it will be.

Hopefully the routes speak for themselves. Feel free to let me know if I've only added to the confusion.

They will no doubt require a little tweaking. I would suggest commenting out the lot, and starting with the final "catch-all" route, to make sure that works for the bulk of your URLs, then you can add the special cases in above it, and test them one at a time.
#5

[eluser]TheFuzzy0ne[/eluser]
Another idea that just popped into my head (which happens from time to time), would it not be easier to have "luxury" as a subdomain, or would that go against your app design? It would certainly solve the routing problems. I'm not quite sure what "luxury" means in the context of your app, so I'm just guessing.
#6

[eluser]Aken[/eluser]
Use a query string for this, that's what it's designed for. Changing your URL structure will end up messing with SEO, confusing visitors, etc.
#7

[eluser]koichirose[/eluser]
Thanks to both of you for your answers.

@TheFuzzyOne: your last solution would have been great, but doesn't work for some urls with parameters.
Code:
$route['(luxury/)?(.+)?'] = "mkt/$2";

It's not playing nice with routes like this:
Code:
$route['stores/:any/(:num)'] = "mkt/stores/single_store/$2";

So I'd have to override them all, I guess.


@Aken: you're probably right, that would be the safest way to go.
I have a problem though: users that click the 'luxury' link will go to market.website.com/?luxury=1 or something.
If the user clicks around, he/she has to stay in the 'luxury' section, thus I'd have to append ?luxury=1 to each and every URL.

How can I do that? I looked around in the url helper but couldn't find anything useful.

Thank you
#8

[eluser]TheFuzzy0ne[/eluser]
Please can you post your routes.php file now? I can't help thinking you might have the routes in the wrong order. Order is important - just like CSS.
#9

[eluser]koichirose[/eluser]
Here's my full routes file.
http://pastie.org/private/35filohyhcaeecmdgu7fw

I have multiple subdomains. IS_MKT is true when the user is at market.website.com .

Many pre-mkt routes are *really* old.

Anyway, I think the query string way is the safest, I just need to figure out how to edit all the urls.
Or, I could use sessions...




Theme © iAndrew 2016 - Forum software by © MyBB