Welcome Guest, Not a member yet? Register   Sign In
Rewrite url or use routing
#1

[eluser]0v3rth3d4wn[/eluser]
Hi! In my application I have the following url: http://mysite.com/bg/category/index/2/12 where category is my controller, index is the method and 2 and 12 are the parameters. 2 is the category id and 12 is the subcategory id. How can I get rid of the index (I tried with routing but no luck) and how can I replace or add the name of the category and the subcategory depending on the id which is the id in the database. I want to achieve something like http://mysite.com/bg/category/clothing/shirts . I'm not sure what I have to use - routing or some .htaccess magic... Thanks.
#2

[eluser]PhilTem[/eluser]
Use routing. Or _remap()-functionality.

Drop off your routes.php code here so we can debug it. In most cases it's just a typo preventing the routes to really work. And please use the [ code ]-tag - without white spaces of course.
#3

[eluser]0v3rth3d4wn[/eluser]
Here is my route.php
Code:
$route['default_controller'] = "home";
$route['404_override'] = '';

// URI like '/eng/about' -> use controller 'about'
$route['^(bg|eng)/(.+)$'] = "$2";

// '/eng' and '/bg' URIs -> use default controller
$route['^(bg|eng)$'] = $route['default_controller'];  

//here first I try to get rid of the index but something isn't just right and it is not working...
$route['(bg|eng)/category/:num/:num'] = "category/index/$3/$4";
#4

[eluser]Aken[/eluser]
Your routes should go in order from most to least specific, and always put custom routes after the default and 404 routes.

Try putting your last route after the 404_override.
#5

[eluser]0v3rth3d4wn[/eluser]
Thanks for the tip, but I still can't get my route to work. I'm not writing it right.
#6

[eluser]Aken[/eluser]
You also need parentheses around :num in order to use the $1/$2/... back references.
#7

[eluser]PhilTem[/eluser]
To sum up previous posts, your routes.php should look like this

Code:
$route['default_controller'] = "home";
$route['404_override'] = '';

//here first I try to get rid of the index but something isn't just right and it is not working...
$route['(bg|eng)/category/(:num)/(:num)'] = "category/index/$3/$4";

// URI like '/eng/about' -> use controller 'about'
$route['^(bg|eng)/(.+)$'] = "$2";

// '/eng' and '/bg' URIs -> use default controller
$route['^(bg|eng)$'] = $route['default_controller'];

If you don't wanna have numeric parts within the /category/-uri, then replace it with either .+ or (:any) and make your category/index-method to translate the given string to a category- or sub-category id respectively.

Code:
//here first I try to get rid of the index but something isn't just right and it is not working...
$route['(bg|eng)/category/(:any)/(:any)'] = "category/index/$3/$4";
#8

[eluser]0v3rth3d4wn[/eluser]
Thank you all for the help and the tips, everything is now working well.
As for the removing the id-s in the url and replacing them with let's say the category name, the only thing that comes to my mind is using an extra unique field for each category(subcategory, product) entry. Let's say I have category with id:1 and name:clothing. I should create a field which will be also "clothing" and will be unique and I will use it as my link instead of the id.
If I am to have two categories with the same name(which won't happen but in the products it may) I should just check if such product exists and if it does I should add the same name with "-1" at the end of the string. I think this is something like a permalink or I'm wrong? Is this a good way to go? Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB