Welcome Guest, Not a member yet? Register   Sign In
non-standard URI routing question /(var)/class/function/seg1
#1

[eluser]ajcolburn[/eluser]
Hi there,

I've just started learning Code Igniter and feel so much more productive already. I have a question regarding URI routing:

I am creating a magazine site which caters for different US States, and I would like to set up URI routing with this pattern: /(state-name-variable)/controller/function/seg1/seg2/etc, I would need to access the state name value from within my controllers.

Is there a quick and easy way to achieve this without writing loads of routing rules? Any help would be greatly appreciated!

Regards,
Alex
#2

[eluser]Michael Wales[/eluser]
Code:
// example.com/tx/controller/method/var
$route['(al|ak|tx|wi)/([a-zA-Z0-9-])/(a-zA-Z0-9-)/(\d+)'] = '$2/$3/$4';
// example.com/tx/controller/method
$route['(al|ak|tx|wi)/([a-zA-Z0-9-])/(a-zA-Z0-9-)'] = '$2/$3';

You could just then pull the state out in the constructor (or in a parent class) and do whatever you want with it. Or, change the route a bit and use that state in all of your methods.
#3

[eluser]ajcolburn[/eluser]
Thanks Michael, that worked perfectly!
#4

[eluser]xwero[/eluser]
The catch all route would be
Code:
$route['(al|ak|tx|wi)(.+)'] = '$2';




Theme © iAndrew 2016 - Forum software by © MyBB