Welcome Guest, Not a member yet? Register   Sign In
Quick Route Question - it doesn't work!
#1

[eluser]timattz[/eluser]
I want to go to "http://example.com/tiger"
and be routed to "http://example.com/pets/view/tiger"

Here is the code that I think should work:
Code:
$route['tiger'] = "pets/view/tiger";

It seems simple enough but it just goes to "http://example.com/pets/".

Any ideas?
#2

[eluser]xzela[/eluser]
this is just a shot in the dark, but why not try this:

Code:
redirect('pets/view/tiger', 'refresh');

Is '/pets/view/tiger' an actual view or are those parameters?
#3

[eluser]timattz[/eluser]
Hey xzela, thanks for the reply,

I'm not sure how I would do that redirect. I want to redirect from the route config.

In ‘/pets/view/tiger’: 'pets' is the controller, 'view' is the view and 'tiger' is a parameter.
#4

[eluser]xzela[/eluser]
If i understand you correctly, this might help (though i have not tested it, so it may not work)

Code:
$routes[(':num)')] = 'pets/view/$1';

and hopefully your controller looks like this:

Code:
class Pets extends Controller {
//... some code here...

  function view($animal) {
  //..some view code here...

  }

//... some more code
}

The routes will pass whatever is entered in the url: http://example.com/<animal> to the view function view(<animal>);

I hope that made sense, and I hope it worked.
#5

[eluser]Colin Williams[/eluser]
The router routes. It does not redirect. I would do the redirect in Apache config, like an .htaccess file.
#6

[eluser]timattz[/eluser]
The docs clearly show that the route file does redirects:

Code:
$route['journals'] = "blogs";
A URL containing the word "journals" in the first segment will be remapped to the "blogs" class.

So the user types in "journals" and is redirected to "blogs" instead. I'm not sure how this is any different form what I am doing.
#7

[eluser]Colin Williams[/eluser]
They use the word "remapped" which is entirely different than "redirected"

Look through Router.php if you don't believe me.
#8

[eluser]timattz[/eluser]
Well, my initial question used the word "route" so I guess I'm all good.

But wording aside the point of all of this is that the example shows something that isn't working. It might just be that I'm not using the most recent version of CI though.

Anyway, I just did it in the htaccess as you suggested.

Thanks for your help guys.
#9

[eluser]Colin Williams[/eluser]
Your idea of how it is supposed to work is the problem. So, the wording does matter. A route like $route['journal'] = 'blog'; means that if a user visits http://www.example.com/index.php/journal, the Blog.php controller is invoked to handle the request. It does not mean that the user is redirected to http://www.example.com/index.php/blog
#10

[eluser]timattz[/eluser]
And that's what I'm looking for.

When someone goes to “http://example.com/tiger” I want it to invoke the "pets" controller with the "view" view and the "tiger" variable. Just like if they went to “http://example.com/pets/view/tiger”.

The initial question may be misleading but I don't care if the URL changes... I just want it to work.

My main goal is to be able to give out the “http://example.com/tiger” URL to customers and have it work as if they were typing in “http://example.com/pets/view/tiger”. The examples in the docs say that routes should be able to accomplish this.




Theme © iAndrew 2016 - Forum software by © MyBB