Welcome Guest, Not a member yet? Register   Sign In
custom users routes? [SOLVED]
#1

[eluser]dootzky[/eluser]
Hi guys,

I developed over 30+ sites in CI, and I love it! Smile
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? Smile

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. Smile

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?
#2

[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
#3

[eluser]bretticus[/eluser]
You need something that is uniform across the board.

Like:
Quote:www.site.com/users/john/
www.site.com/users/barbara/

then you can at least use something like:

Code:
$route['^users/([^/]+)/(.*)$'] = '$2/$1';
// this is untested and quickly off the top of my head. For demonstration only.

Or you can use wildcard subdomains:

Quote:john.site.com
barbara.site.com
#4

[eluser]dootzky[/eluser]
Thanks for your help guys, but I don't think that will do the trick. Smile

It amazes me how something this obviously needed is so hard for me (us?) to figure out? Smile
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! Smile
Dushan
#5

[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. Smile

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. Smile

Good luck!
#6

[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.
#7

[eluser]dootzky[/eluser]
no offense taken my friend Smile

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
$this->uri->_fetch_uri_string();


// HACK BY DUSHAN
// ----------------------
$store_name_end_pos = strpos($this->uri->uri_string, "/", 1);
$store_name = substr($this->uri->uri_string, 0, $store_name_end_pos);
$store_name = str_replace('/', '', $store_name); // remove all "/"
$this->config->set_item('store_name', $store_name); // so that I could use this later in the code
$this->uri->uri_string = substr($this->uri->uri_string, $store_name_end_pos); // remove this /$store_name/ from CI routes logic

...

I created /libraries/MY_Router.php
so that I could track this change easily in the future, but that's bonus Smile

so far - it's working. Smile

thanks for your help, time, and effort guys, all of you, really. Smile
I hope this will help more users in time.

Cheers,
Dushan
#8

[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.)
#9

[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
#10

[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
  $route["default_controller"]      = "welcome_page";

// test for specific controllers
$x=array("(:any)",'about', 'contact_us','terms','blog','subscribe','specific_controler');
  foreach($x as $xx):
   $route[$xx] = "$1";
  endforeach;

// fall-through to your login section
// finally the Daddy of them all
  $route["(:any) = "login/$1"; // extract using $this->uri->uri_string() or segment(...)

 
 




Theme © iAndrew 2016 - Forum software by © MyBB