Welcome Guest, Not a member yet? Register   Sign In
Routing, regular expressions and category paths...
#1

[eluser]Maarten Troonbeeckx[/eluser]
You guys,

I'm trying to parse the following URLs:

Code:
http://my.shop.be/browse/electronics/hifi
http://my.shop.be/browse/electronics/hifi/00020/sony-bravia

The first one shows all products in subcategory hifi (parent category is electronics).
The second one show the product Sony Bravia with productID = 20.

My routes look like this (RegExhibit to the rescue Wink):

Code:
$route['browse/([a-zA-Z0_9\/]+)'] = 'browse/category/$1';
$route['browse/([a-zA-Z0_9\/]+)/([0-9]+)/[a-zA-Z0-9]+'] = 'browse/product/$2/$1';

My browse controller is setup like this:

Code:
function category($path);
function product($productID, $path)

Problem is that, instead of one parameter $path which equals 'electronics/hifi' in the both functions, it is passed as two parameters, i.e. 'electronics' and 'hifi'. This is because of this line of code in the Router class on line 289:

Code:
$this->_set_request(explode('/', $val));

Is this maybe a situation where _remap() comes into place?
Any ideas on how I can get 'electronics/hifi' passed into just one parameter $path in my controller functions?

Tnx in advance.

M.
#2

[eluser]mddd[/eluser]
You could get the variables in the functions themselves:
Code:
function category()
{
  $path = $this->uri->segment(3) . '/' . $this->uri->segment(4);
  ...
}

function product()
{
  $product_id = $this->uri->segment(3);
  $path = $this->uri->segment(4) . '/' . $this->uri->segment(5);
  ...
}
#3

[eluser]Jelmer[/eluser]
Also, for more general debugging, try this order for your routes:
Code:
$route['browse/([a-zA-Z0-9\/]+)/([0-9]+)/[a-zA-Z0-9]+'] = 'browse/product/$2/$1';
$route['browse/([a-zA-Z0-9\/]+)'] = 'browse/category/$1';
I changed the order and you had it "0_9" within the rule, that would match "0", "_" and "9" and I'm assuming you meant "0-9" (0 to 9 numeric).

The routes are matched against the URI in the order you define them in. So the rule you put first (I put second above) would already match the URI before the other rule would ever be considered.

If you got a very specific rule and a more general catch-all type rule: always put the specific rule before the general rule.
#4

[eluser]tranc huc[/eluser]
thanks
#5

[eluser]Maarten Troonbeeckx[/eluser]
I can work with that Wink
Tnx!
#6

[eluser]Maarten Troonbeeckx[/eluser]
I've implemented it like this:

Code:
public function _remap($method)
{
    $rsegments = $this->uri->rsegment_array();
        
    if ('category' === $method)
    {
        $this->category(implode('/', array_slice($rsegments, 2)));
    }
    elseif ('product' === $method)
    {
        $productID = intval($rsegments[3]);
        $path = implode('/', array_slice($rsegments, 3));
        
        $this->product($productID, $path);
    }
    else
    {
        $this->index();
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB