Welcome Guest, Not a member yet? Register   Sign In
Problem with Routes
#1

[eluser]neen[/eluser]
Hi guys,

I have setup routes like follows:

$route['user/:any'] = 'user';
$route[':any'] = 'content/view';

I want all urls passed to content/view, with the exception of /user...However, when I call user/login (I have it set to print a simple test message), I get a completely blank page (though there are no errors).

When I remove both routes, it works fine. Is there any other (or better) way to do this?
#2

[eluser]xwero[/eluser]
Code:
$route[’user/(:any)’] = ‘user/$1’;
Will do the trick. You have to pass the fetched string to the actual controller.
#3

[eluser]neen[/eluser]
Thanks xwero...

So I have to have two routes for every other controller I have...ie:
Code:
$route['user/(:any)'] = 'user/$1';
$route['user'] = 'user';
No way to put those into just 1 route?
#4

[eluser]xwero[/eluser]
yes
Code:
$route['user(.*)'] = 'user$1';
#5

[eluser]neen[/eluser]
Much thanks, xwero!
#6

[eluser]dootzky[/eluser]
wait, so that means, that if I had an old link in the site, which used $_GET parameters for ID of the article, like this:

Code:
www.mysite.com/article.php?aid=84

I could add a route rule to redirect that old link to this new URL, which is written in CI:

Code:
www.mysite.com/article/84

Is that possible? :| Oh, man, that would save me hours and hours of coding.. Is this the good way to go? Maybe something like:

Code:
$route['article.php/:any'] = "article/index/$1";

this line above in the routes just gives me and 404 page.
what am I doing wrong? Sad
#7

[eluser]Pascal Kriete[/eluser]
It's a little tricky because of how CI resolves the request. Regardless, you would be better off doing a redirect like that on a server level.

Does your host support mod_rewrite? Something like this would do what you want:
Code:
RewriteRule ^/article\.php?aid=([0-9]+)$ /article/index/$1 [R]
#8

[eluser]neen[/eluser]
dootzky: that is how CI is intended to be used...here is how to do it...

First, you would do this in your route:

Code:
$route['article/:any'] = 'article';

Then, in your index function...

Code:
public function index($id)
{
    // get article here
    // call view, passing article info    
}

Please note, the route above is relative to your index.php file...ie: in the URL, you would have:

http://www.site.com/index.php/article/3

If you are using .htaccess, it would be...

http://www.site.com/article/3

Edit: the above post should work as well...

It is important to note, when editing CI routes, the only time you will use the $1 variable is when you have a regular expression as a route. Otherwise, CI will automatically pass the part of the route matching :any, :num, etc at the end of the 'redirected' route.
#9

[eluser]dootzky[/eluser]
hi guys, first of all - thanks for your time and effort!
now, back to the problem -> I still can't get it to do what I want it to do.

@inparo
how should my .htaccess file then look like? I'm already using it for hiding the "index.php" file.
this is my current .htaccess file:

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|public|user_guide|robots\.txt)
RewriteRule ^(.*)$ /domino/index.php?/$1 [L]

where should I put your RewriteRule line? At the end? As the second line? I've tried it all positions (first line, second line, third line, and at the end), no luck again. None of those work. Do I need another RewriteCond perhpas? o.O


@neen
this $route didn't do the trick, or perhaps I misunderstood you?
I'm hidding the index.php file with .htaccess, and it all works totally ok, and when I add your route:

Code:
$route['article/:any'] = 'article';

the URL:
www.site.com/article/84
won't work.

before that, I was using this route, for URL shortening:

Code:
$route['article/(\d+)'] = "article/index/$1";


and that worked.

just to get me 100%: your route "works", but I can't get the $article_id in the "function index($id)" function. :|
it just doesn't pass that parameter. I var_dump the $ID I should get trough index() parameter, and it's NULL.

Am I doing something wrong? Perhaps I misunderstood your instructions somewhere? :|


and once again -> THANK YOU BOTH FOR YOUR TIME AND EFFORT, I REALLY APPRECIATE IT!
#10

[eluser]neen[/eluser]
Make sure you have the proper case...variable names in PHP are case sensitive, so $ID is a different variable than $id. (you can name the variable whatever you want in the function declaration, though)

Try this for your route:
Code:
$route['article/:num'] = 'article/index';

Then dump the ID again and tell us what you see...




Theme © iAndrew 2016 - Forum software by © MyBB