Welcome Guest, Not a member yet? Register   Sign In
Extensive use with the URI Routing

Hello Everyone, I am about to begin another project with this Great framework when I realized 'would there be an easier way to do this with CI?'. Let explain...

I am trying to develop a really dynamic site with CI. I would like to have my domain be like: http://www.example.com/(username) where "(username)" will the username of the client. (I will make sure that the client has a proper username with regular expression to make sure the url looks pretty).

I am able to do this in the config/route.php file like this:
$route["(:any)"] = "client/lookup/$1";

I have been successful in setting this up, but then it also destroys my Controllers. I can work around this by doing something like:

$route["welcome"] = "welcome";
$route["welcome/([a-zA-Z]+)"] = "welcome/$1";

But then I would have to do this for ALL Controllers. Would there be an easier way to work around this?

Many thanks CI's

I wouldn't have thought so, because CodeIgniter wouldn't be able to tell the difference between a client name and a controller, You could probably get around this with a database query (which would probably be a bit difficult to do at such an early point in execution), but then things would mess up if a client happens to have the same name as a controller.

Another way to do it might be to redirect to the client lookup method if there's only a single URI parameter, but it's hardly fool proof.

That is kind of what I thought... I thought I was breaking it in a sense.

Thank you for the fast reply Fuzzy.

if you've only got a couple of controllers, then I'd specify the controller name and any other path segments in the routes file before the usernam rerouting
If you're going to have too many controllers to do that, you could maybe write a function to check the filenames of the controller directory, and if you can't find a match there, assume its a username (would have to prevent people regisering the name of a controller)

But I definatly think the first solution is much cleaner

Is this really necessary?
$route["welcome"] = "welcome";
$route["welcome/([a-zA-Z]+)"] = "welcome/$1";

It doesn't work with just: [?]
$route["welcome/([a-zA-Z]+)"] = "welcome/$1";

gregmax17 limit the username as much as possible. If the username can only contain alphanumeric characters and has to be between 4 and 100 characters long the route would be
$route['([a-zA-Z0-9]{4,100})'] = “client/lookup/$1”;
Of course there will be controllers that match the regex but now you only have to route those that are getting called with no other segments.
$route['(blog|shop)'] =  '$1';
If you think this is still too much routing you can use an identifying prefix, like the old days tilde.
$route['~([a-zA-Z0-9]{4,100})'] = “client/lookup/$1”;

Theme © iAndrew 2016 - Forum software by © MyBB