Welcome Guest, Not a member yet? Register   Sign In
routes problem ? (thx for reading)
#1

[eluser]starenka[/eluser]
Hi,

trying to implements easy multilang site with uris as:

/cs/user/join (or just /user/join)
/sk/user/join
etc etc..

so i added a route:

Code:
$route['(cs|sk|pl)/(.*)'] = "$2/$1";

but when i try to get the language segment in controller...

Code:
class User extends Controller
{
    
     function join($language)
     {
         var_dump($language);
     }    
}

i allways get "the method" segment...

any clues? or more elegant solutions?
#2

[eluser]eggshape[/eluser]
how strange...have you tried this

Code:
$route['([cs|sk|pl])/(.*)'] = "$2/$1";

oh i should also mention that you can write your uri like $method/$language, and then in your method use segment():

Code:
public function join()
{
  $language = $this->uri->segment(1,'cs'); // and that will default to 'cs' language
}
#3

[eluser]starenka[/eluser]
hi. thx for reply...

ad regexp: the regexp seems to be okay (why should i use []?) - your regexp leads to "page not found"
ad uri: i know, but i like /$lang/$class/$method/$params better...
#4

[eluser]eggshape[/eluser]
sorry I really don't know why your regexp failed; seems like *proper* regexp lingo, so maybe it's how CI handles this.

the brackets aren't really necessary; in regexp-speak, they just mean "only these cases are valid"...like match [1a,] (match 1 or a or ,). since it fails, it must be the way CI handles these regexp for this class.

edit: maybe you have an extra forward slash for $2; i would also change the * to + (1 or more)
Code:
$route['(cs|sk|pl)/(.+)/$'] = "$2/$1";

if your regexp fails to match the URI, then CI uses the URI as the default.
#5

[eluser]Michael Ekoka[/eluser]
I'll try to give a crack to your problem. I'm writing this from the top of my head here so this may or may not work at the first trial. Two problems that i see with your regex:

1-) the regex syntax is wrong or at least it will not behave as expected. I'll try to explain what your regex is doing at the moment:
what you have:
Code:
$route['([cs|sk|pl])/(.*)'] = "$2/$1";
// - check if i have 1 character from this list {cs|kpl} 1 time: [cs|sk|pl]
// - group the resulting character and backreference it with $1: ([cs|sk|pl])
// - followed it by a slash: ([cs|sk|pl])/
// - followed by any character 0 or more times: ([cs|sk|pl])/.*
// - group the "any character 0 or more times" to backreference it with $2: ([cs|sk|pl])/(.*)
what you want:
Code:
$route['(cs|sk|pl)/(.*)'] = "$2/$1";
// - check if i have one of these strings "cs","sk" or "pl" 1 time and backref it to $1: (cs|sk|pl)
// - followed by a slash: (cs|sk|pl)/
// - followed by any character 0 or more times, grouped to be backrefed with $2: (cs|sk|pl)/(.*)

2-) Second problem: your language code is not made optional, if you don't include it in the url, your routing may give some unexpected results.
what you actually want:
Code:
$route['(?:(cs|sk|pl)/)?(.*)'] = "$2/$1";
// - check if i have one of these strings "cs","sk" or "pl" 1 time and followed by a slash: (cs|sk|pl)/
// - whatever is in between the parentheses is considered to be the first backreference $1.
// - Now, group the whole inside non-capturing parenthesis:  (?:(cs|sk|pl)/), that way i don't need to back reference this second outer group.
// - Now, make the entire latest group optional: (?:(cs|sk|pl)/)?
// - that group, if it is set is followed by any character 0 or more times: (?:(cs|sk|pl)/)?.*
// - Now, group the "any character 0 or more times" in capturing parenthesis to make it the second backref: (?:(cs|sk|pl)/)?(.*)

I have posted something similar about internationalization via uri and routing, if you're interested go here.
#6

[eluser]starenka[/eluser]
hi!

heh what to say. look @ my initial post. it's the same regexp you write Smile (ok except the optional thing, but i will pass the lang allways)

maybe i found what's wrong about this. debugged the the Router class a bit and it re-routes my regexp just fine (from $lang/$class/$method to $class/$method/$lang) but then _reindex_segments() screw this big time... dunno if this a normal behaviour, but i realized i don't even need the route. i will just use $this->uri->segment(1) Smile)
#7

[eluser]Michael Ekoka[/eluser]
:lol: my bad, I looked at the wrong regex. It would've saved me 10 minutes.

Well in theory your route should work, but i suspect that the source of your problem has to do with this.

And yeah, you don't need to include the language code in the route. $this->segment(1) should do just fine.
#8

[eluser]Armchair Samurai[/eluser]
Since you're re-routing things, try using the the ruri functions

Code:
class User extends Controller
{
    
     function join()
     {
         var_dump($this->uri->rsegment(3));
     }    
}




Theme © iAndrew 2016 - Forum software by © MyBB