1
votes

I am using Codeigniter for my project. I need to get URL structure like this:

Main product page

example.com/language-prefix/products/

Select products by category

example.com/language-prefix/products/category

Select products by category AND sub-category:

example.com/language-prefix/products/category/sub-category

Select specifict product under category AND sub-category:

example.com/language-prefix/products/category/sub-category/product-name

OR only under category

example.com/language-prefix/products/category/product-name

Question is - what would be a good solution for this? Because problem starts here:

example.com/language-prefix/products/category/what-ever

what-ever can be a product or a sub-category and how to decide - what data and view should be returned?

In my DB table structure I have many-to-many relationships between products and categories. It means, than one product can be assigned to many categories. Every category has a self refferecing foreign key (parent).

Maybe I need to get some restrictions for category adding for products? Or specifing a main category or what?

I have couple of ideas:

  1. Keep every route for category/sub-category and product in DB for example:

/products/watches/for-men/ /products/watches/for-men/rolex-abc-whatever-product

And so on, and so on. But this, I have feeling could be very slow.

  1. Make a simple route to products controller -> method view(), and in the method go trough all passed segments and when it comes to

/products/category/what-ever

then first check if there exists such product, if true, then return product_view and data, if not, check if there is such sub-category and if there is, then return the product grid or return 404 if there is not.

I assume a simple solution could be just keep all categories and sub-categories after /products/ and add category-id like

example.com/language-prefix/products/1-watches

example.com/language-prefix/products/2-for-men

But I hope there is better solution for this.

Also I cant figure out this:

example.com/language-prefix/products/category/sub-category/product-name

I need to return product with name product-name AND check if it is under those two categories so URL for example:

example.com/language-prefix/products/hello-kity/bla-bla/product-name

would NOT return that product.

Any better/other solutions?

1
i only don't understand this last part after the line "Also I cant figure out this...", can elaborate please. what do you mean by it would not return that product? - PK.
any idea for hide language-prefix from browser url? - Mayur Kukadiya

1 Answers

0
votes

"Keep every route for category/sub-category and product in DB"

the idea above won't work well in CI and you'll probably break the way CI routing works. remember that the route points you to a controller/function. only when you get there can you start calling the db... so this idea will be weird since you gotta' call the db first to match the routes.

"Make a simple route to products controller -> method view(), and in the method go trough all passed segments and when it comes to /products/category/what-ever"

the idea above is better, you could have a route like this $route['[a-z]{2}/products/(:any)'] = "product/index/$1"; so http://www.example.com/sg/products/gems/ruby and http://www.example.com/sg/products/gems/red/ruby will both go to the product controller's index function

segment(4) can be either "red" which is a sub-category or "ruby" which is a product. segment(3) is always going to be a category. segment(5) is either empty or a product. you must now simply decide if products or sub-categories that precedence, better still handle within your code logic that they never will collide. i.e. product and category/sub-category names cannot be duplicated.

public function test($category = NULL, $sub_category_or_product = NULL, $product = NULL)
{
    echo $category.'<br />'; // segment(3) is always your category
    if ($product)
    {
        echo $sub_category_or_product.'<br />'; // segment(4) is a sub-category
        echo $product.'<br />'; // segment(5) is a product
    }
    else
    {
        echo $sub_category_or_product.'<br />'; // it's a sub-category or product
        // call the db and test whether its a sub-category
        // then test if its a product... or the other way around
    }
}

of cuz this is just one of the many possible ways to solve it... however if you don't have to stick with that URL structure... i would suggest you put products/categories/sub-categories under the "/products/" namespace in the URL for "listing view". and put products under the "/p/something" namespace for the details view

www.example.com/p/ruby // product details
www.example.com/products // product listing
www.example.com/products/gems // product listing
www.example.com/products/gems/red // product listing

for example see:

http://www.beautylish.com/products
http://www.beautylish.com/p/mac-mineralize-skinfinish-natural

this way you won't have to worry about categories/sub-categories names clashing with product names

people probably want to put the cats and sub cats in the product url for seo... but if you check beautylish they got very damn good seo without choking up the url and simply putting only the product name, it's also shorter and more readable.