Welcome Guest, Not a member yet? Register   Sign In
Routes not rewriting links properly
#1

[eluser]steeped[/eluser]
Hey!

I've just built my very first site in CI and I really enjoyed using the framework. I just ran into this problem and haven't been able to find a solution.

My URL ends up looking something like example.com/index.php/electronics/notebook/toshiba123 - but I would of course like to simplify it to something like example.com/index.php/item/toshiba.

I try to route using this: $route['item/(:any)'] = "electronics/notebook/$1";

It sorta work. When I go to example.com/index.php/item/toshiba123, the page will load all items inside the electronics class, so not only does toshiba123 appear, but so does everything else.

Any idea what i'm doing wrong?
#2

[eluser]jvicab[/eluser]
try example.com/index.php/item/toshiba/123
If you want a cleaner url, use rewrite mode to remove index.php from your url
#3

[eluser]steeped[/eluser]
[quote author="jvicab" date="1313175342"]try example.com/index.php/item/toshiba/123
If you want a cleaner url, use rewrite mode to remove index.php from your url[/quote]

toshiba123 is it's own segment in itself - but non-the-less I tried since I don't have any better ideas and it didn't work Sad
#4

[eluser]jvicab[/eluser]
what are your implementation? having a controller named item or a function named item inside default controller?
Any of these two ways should work:

in a default controller, having a function named items which expects a parameter that will be what you pass after, toshiba123 in this case
or having a controller named items and inside it having a index function which is called by befault of no other function name is found and as before, expecting a parameter, that is toshiba123
#5

[eluser]steeped[/eluser]
[quote author="jvicab" date="1313176265"]what are your implementation? having a controller named item or a function named item inside default controller?
Any of these two ways should work:

in a default controller, having a function named items which expects a parameter that will be what you pass after, toshiba123 in this case
or having a controller named items and inside it having a index function which is called by befault of no other function name is found and as before, expecting a parameter, that is toshiba123[/quote]

I have electronics which is the controller, and notebook with is the method. The method loads the model which is based on the third segment (which in this case is toshiba123). If the third segment is HP123, it will load the HP123 information.
#6

[eluser]jvicab[/eluser]
if what you want is convert example.com/index.php/electronics/notebook/$1 into example.com/index.php/item/toshiba try
$route['electronics/(:any)/(:any)']='items/$2';
this rewrite the url changing electronics(the name of your controller) by items, and append the third parameter. Again, I am not sure what you would like your url to be
#7

[eluser]steeped[/eluser]
[quote author="jvicab" date="1313177435"]if what you want is convert example.com/index.php/electronics/notebook/$1 into example.com/index.php/item/toshiba try
$route['electronics/(:any)/(:any)']='items/$2';
this rewrite the url changing electronics(the name of your controller) by items, and append the third parameter. Again, I am not sure what you would like your url to be[/quote]

The current long URL which I have is: example.com/index.php/electronics/notebook/toshiba123, but I would like to simplify it to instead look like: example.com/index.php/item/toshiba.

I apologize if I'm confusing you a little - I think i'm starting to confuse myself a little too.
#8

[eluser]jvicab[/eluser]
don't worry, all of us most of the time get confused coding, that ios why we come to this forum for help right?
I am not a guru in CI but I do like to help as much as I can, so to get items in the url should not be dificult, as I told you before, but putting only toshiba is another thing, because how CI will know it? I am assuming you may have toshiba123, 124, and more, right? the only way is to define (:any) for it but it will rewrite toshiba for any parameter, which probably is not a good idea. maybe a good solution is to put in the url items, followed by the notebook parameter and hide the third which is the model, right?
#9

[eluser]steeped[/eluser]
[quote author="jvicab" date="1313178608"]don't worry, all of us most of the time get confused coding, that ios why we come to this forum for help right?
I am not a guru in CI but I do like to help as much as I can, so to get items in the url should not be dificult, as I told you before, but putting only toshiba is another thing, because how CI will know it? I am assuming you may have toshiba123, 124, and more, right? the only way is to define (:any) for it but it will rewrite toshiba for any parameter, which probably is not a good idea. maybe a good solution is to put in the url items, followed by the notebook parameter and hide the third which is the model, right?[/quote]

toshiba123 was only an example. The structure is index.php/electonics/notebook/anyItem. If course anyItem can be anything, and for an example I jusg randomly chose toshiba123.

That works, and when the user goes to index.php/electonics/notebook/anyItem, the page loads properly.

But the URL is really large and I would like it to be smaller, so I beleive I need to use routes. I just don't know how to properly structure it so instead of having to type that long index.php/electonics/notebook/anyItem URL, you would just have to type index.php/item/anyItem. And I'll be getting rid of the index.php at a later time - I can do that with .htaccess.

Thanks for your help. If you would like, I can PM you the working site so you can see how its set up.




Theme © iAndrew 2016 - Forum software by © MyBB