custom users routes? [SOLVED] |
[eluser]dootzky[/eluser]
Hi guys, I developed over 30+ sites in CI, and I love it! Now, I have a specific request, which is giving me routes-problem. There is this site, let's call it: www.site.com ok, now, every USER on that site, should have a custom URL, for *all of his pages*. Here's an example: www.site.com/john/ www.site.com/barbara/ etc. ok, so - this would be easy, if it was a "single page for each user", so I could easily rewrite /config/routes.php to interpret something like this: www.site.com/profile/john/ -> www.site.com/john/ www.site.com/profile/barbara/ -> www.site.com/barbara/ etc. ok, that would be easy. BUT, what I really need to do, is to make their username *part of their URL*, so that they have other pages, like they have multiple CI instances, but it's only a URL trick. So, I need to be able to route these pages as well: www.site.com/john/contact_us www.site.com/john/product/123 www.site.com/john/news www.site.com/barbara/contact_us www.site.com/barbara/news etc. get it? So, basically, what I need to do is: to have only one instance of my CI, and to find a way to put username in the URL, so that it acts like it's a base_url(), but the real application code is in the root of the "www.site.com", of course. Is that doable? :| I know that I can add suffix on every URL, transfering their username that way, for example: www.site.com/contact_us/john www.site.com/contact_us/barbara etc. but I don't want that, I need to make the URLs very user-friendly so that they can share their "mini websites" with short and simple URLs. Any way for this to be done? Thanks for your time and effort upfront guys, I really appreciate it. Cheers from Europe, Dushan p.s. perhaps something can be done similar to the "multilanguage URL hack" like in the CI Wiki? http://codeigniter.com/wiki/i18n_Multi-l...ry_Helper/ ?? Something like that?
[eluser]tonanbarbarian[/eluser]
if using apache should be possible to do a url rewrite to change from www.site.com/contact_us/john although actually you probably need to change the url to something like this www.site.com/contact_us/index/user/john
[eluser]bretticus[/eluser]
You need something that is uniform across the board. Like: Quote:www.site.com/users/john/ then you can at least use something like: Code: $route['^users/([^/]+)/(.*)$'] = '$2/$1'; Or you can use wildcard subdomains: Quote:john.site.com
[eluser]dootzky[/eluser]
Thanks for your help guys, but I don't think that will do the trick. It amazes me how something this obviously needed is so hard for me (us?) to figure out? I can almost bet that there is a way to squeeze one prefixed parameter in URL, like with the language Lib, and that's what I must focus on. That, or one copy of .htaccess in default folder for each user? Would that work?? something like: /public_html/public/users/john/.htaccess /public_html/public/users/barbara/.htaccess and the real CI code would be here, of course: /public_html/index.php ? hmmm.. I wonder if this could do the trick. I'll test it over the weekend, and hit you back if I get any good results. Thanks again! Dushan
[eluser]bretticus[/eluser]
Quote:It amazes me how something this obviously needed is so hard for me (us?) to figure out? No offense, but I didn't put much thought into my suggestion other than stating the obvious. It's your project and, thus, everyone here is expecting you to put the majority of the thought into your problem. I have looked at the language lib you referenced yesterday. It has a custom routing rule that looks for a two-letter prefixed parameter. Unless you can be assured that all your users will have two-letter usernames, you might need some similar uniformity in your URL schema. Personally, I'd just use subdomains and abstract all this routing business out of the problem. Good luck!
[eluser]WanWizard[/eluser]
Create a hook or a library extension that extracts the first segment from the URI, and checks if it's a valid username. If so, store it somewhere (a config variable perhaps) where it can be retrieved by your controllers. Then strip the segment from the URI, so that routing happens on the next segments. If not, show an error message, or redirect to a generic page.
[eluser]dootzky[/eluser]
no offense taken my friend however, I found a way (hack) how to achieve this. I edited file /libraries/Router.php, and after this line: Code: // Fetch the complete URI string I created /libraries/MY_Router.php so that I could track this change easily in the future, but that's bonus so far - it's working. thanks for your help, time, and effort guys, all of you, really. I hope this will help more users in time. Cheers, Dushan
[eluser]bretticus[/eluser]
Nice hack Dustin. I love this about codeigniter. It's fairly easy to modify. Just wondering, will all your URL's have a username? I like WanWizards approach of checking for a valid username. I know that's a lot of overhead, but you could cache usernames in memory (memcache or APC, etc.) and bypass your routing code when the username is invalid (and thus use URI paths where a username is not required.) If you actually need to do this, you might want to make sure that your users can never register a username that you ever anticipate using in your URL's. Glad you figured it (WanWizard gave much better advice, as usual.)
[eluser]Unknown[/eluser]
i added some custom_routing in config/routes $route['(:any).?([^exclude_controller])'] = 'users/controller_to_view_user_page'; Catch the additional parameter on URI, to give the right user pages. I don't know, is this a good practice or not. buat somehow it works. It's amaze me how i could do this regex thing.. im totally noobs
[eluser]John_Betong_002[/eluser]
If I understand you correctly then I think you can use the standard config/routes.php ./application/config/routes.php Code: // Set your default path |
Welcome Guest, Not a member yet? Register Sign In |