Welcome Guest, Not a member yet? Register   Sign In
route a string to a specific controller
#1

[eluser]FirstByte[/eluser]
Hi
i'm developing a small social networking application, but right now I'm stuck with some URI Routing.
right now i have different controllers for displaying static pages, contact us, faq, profile, account they are working great they are looking something like:
exemple.com/pages
exemple.com/contact_us
exemple.com/myaccount

when i want to display the user profile the URI is something like
exemple.com/profile/username1
exemple.com/profile/username2

the thing that I'm trying to achieve is that when the uri will be something like
exemple.com/username1 will need to route to exemple.com/profile/username1
exemple.com/username2 will need to route to exemple.com/profile/username2

but still the other controllers will need to work as they are now.

I heaved try to do this using regex using the method "Positive and Negative Look behind" but with no success.
Does anyone can suggest a good way to accomplish my uri routing

regards
#2

[eluser]TheFuzzy0ne[/eluser]
You'd need to set the routes for contact us, my account and so on, all before one final catch all route:
Code:
$route['(.+)'] = 'profile/username';

Be warned, however, with your plan of action things will go very wrong if any of your users sign up with a username that matches one of those module names. You should really think about this before implementing it.
#3

[eluser]FirstByte[/eluser]
Right now I'm using the solution that you already posted, and i thank you for your time, but i'm searching for a a way to do this routing just from a single condition this is way i haved try to use some advanced regex but this is one of the chapters that I'm not so good with.

Yes, i already know about this problem, but this is what my client want, also on the registration controller i already blocked the possibility of registration with the name of one of the of the controller names.
#4

[eluser]Colin Williams[/eluser]
There's no magical regex for usernames because they can match almost any pattern (hence the use of '.+'). You would have to introduce a convention, like a prefix 'user_', but at this point you might as well have 'user/username'.
#5

[eluser]FirstByte[/eluser]
after some searches i haved started to use this for now i see that is working, i dont know what will happend in the future.

Code:
//
$route['mycontroller'] = 'mycontroller';
$route['mysecondcontroller'] = 'mysecondcontroller';
//this is for additional URI segments
$route['(mycontroller|mysecondcontroller)(:any)'] = '$1$2';
$route['(:any)'] = 'profile/view/$1'

hope will help someone
regards
#6

[eluser]xwero[/eluser]
A simple solution is to add a tilde (~) in front of the username which you can search for in the regex.
#7

[eluser]FirstByte[/eluser]
yes i know, but still i need to fallow the specs requirements.
thanks for your help guys
#8

[eluser]xwero[/eluser]
You better use the regex pattern instead of using the keyword any build-in the CI routing class. Try to be as specific as possible. For example you shouldn't allow the usernames to contain a forward slash this will make it easier to determine where the username ends.
Code:
$route['([a-zA-Z0-9]+)'] = 'profile/view/$1';
Urls like controller/method will not match the pattern.

If segments precede the username try to have at least 2 which means routing isn't needed.




Theme © iAndrew 2016 - Forum software by © MyBB