custom url to username and maintain urls for controllers |
[eluser]asumaran[/eluser]
I want to assign a vanity url (like facebook) to any registered user in my app just like: Code: site.com/username Code: $route['([a-zA-Z0-9_-]+)'] = "user/profile/$1"; Code: site.com/controller I tried putting adding more routes in routes.php like this: Code: /* Exceptions */ this needs more rules for every controller. this is not elegant way to resolve it, other way would be assign to user a URL like site.com/users/username yes, but I like the short way ![]() PS. -- sorry my english ;-)
[eluser]Dam1an[/eluser]
Take a look at thus router extension If the first segment maps to a controller, it will execute the controller as normal, otherwise, it will default to a fallback controller I use it for basic CMS functionaity in my sites
[eluser]asumaran[/eluser]
I had solved my problem this way, maybe it isn't the best way, but it works config/routes.php Code: $url_parts = explode('/',$_SERVER['REQUEST_URI']); I will try the other way ;-)
[eluser]Unknown[/eluser]
^how is this approach different or better than just specifically defining the reserved controller routes like this: $route['tv'] = "tv"; $route['radio'] = "radio"; $route['students'] = "students"; $route['media'] = "media"; is there anything wrong with my approach? i am curious... as i want to implement it and want to know the best possible way.
[eluser]philpalmieri[/eluser]
HI, my approach was similar http://philpalmieri.com/2010/04/personalized-user-vanity-urls-in-codeigniter/
[eluser]Unknown[/eluser]
Hi, here is a simple way... $route['404_override'] = 'user/profile'; // Just add your controller/function in case controller not found ![]() Apply a condition while creation of account or blog so your code should check all controller's file name if any controller exists then alert user that this username is not available.. ![]() Thanks Regards
[eluser]InsiteFX[/eluser]
Posting to a 3 year old topic? Plus there is a better way to do it, welcome controller: Code: public function index($username = NULL)
[eluser]Aken[/eluser]
That way isn't better, because then every single one of your controller methods needs to be in that welcome controller. You wouldn't be able to use other controllers without adding them to the routes. The 404 controller is the easiest way to use CI controllers like normal, and then a catch-all wildcard controller for things like usernames. CI isn't really set up to handle first-segment wildcards properly, which will probably be addressed in the future. For the time being, it's the best solution. |
Welcome Guest, Not a member yet? Register Sign In |