Welcome Guest, Not a member yet? Register   Sign In
using ci to route domain.com/variable_here ?
#1

[eluser]tkyy[/eluser]
hey friends, i am making a new social media site and it requires that i redirect the portion of the domain after the actual domain extension to a controller function.

so i want people to be able to register as www.myapp.com/doug instead of having to use a remap on the user controller and having www.myapp.com/user/doug. at first i thought it would be a simple routing problem but haven't been able to solve this one without conflicting with something else.

my guess is some kind of code needs written that validates the signup as well to make sure they are not taking any controller names as they choose a username. can someone please give me an example or link me to a thread where this effect has been achieved (not the validation, but the actual routing)
#2

[eluser]pickupman[/eluser]
Routes work like mod_rewrite. First come, first serve. When creating a $route['(:any)'], you need to absolutely sure you have everything routed some way to protect against this before this rule. As you also mentioned, you will need to prevent any user creating a name that matches a controller name. Maybe a [url="http://ellislab.com/codeigniter/user-guide/general/hooks.html"]hook[/url] could be used to check if user name exists before sending to controller. If user exists redirect url to /user/username, otherwise process like normal. Then you may not need to have a (:any) route.
#3

[eluser]tkyy[/eluser]
so you will have to add to the actual routes file with each user that registers, you can't just add one general rule? it seems like a sort of sloppy way to do it. maybe have a library that stores all the usernames seperate with the user id's and rebuilds the routes file each time someone registers would work well?

the validation wouldn't be a problem, i would just scan the directory and make sure they weren't taking controller or folder names, or anyone else's username obviously.
#4

[eluser]wh1tel1te[/eluser]
One way to do it is to write the dynamic routes to a file, then in the routes.php config, get the file contents and use it.

1. Get usernames and write them to a file (this would most likely go into your controller):
Code:
<?php
$users = array(
'david',
'sandra',
'bobby',
'claire',
);
$routes = serialize($users);
file_put_contents(APPPATH . 'user_routes.php', $routes);
?>

2. Get user routes into routes.php config
Code:
<?php
$user_routes = unserialize(file_get_contents(APPPATH . 'user_routes.php'));
foreach($user_routes as $user)
{
    $route[$user . '(:any)'] = 'user/' . $user . '$1';
}
?>

Disclaimer: I wrote this during my lunch break at work, so there may be bugs Wink
#5

[eluser]pickupman[/eluser]
You would need to build routes specifying controllers first then have (:any) rule last.

Code:
$route['controller1/(:any)]' = 'controller1/$1';
$route['controller2/(:any)]' = 'controller2/$2';
//etc or could do this dynamically by reading directory then loop to create routes

//Add last
$route['(:any)'] = 'user/$1'; //route any uri not above to the user controller.
#6

[eluser]tkyy[/eluser]
[quote author="pickupman" date="1279174417"]You would need to build routes specifying controllers first then have (:any) rule last.

Code:
$route['controller1/(:any)]' = 'controller1/$1';
$route['controller2/(:any)]' = 'controller2/$2';
//etc or could do this dynamically by reading directory then loop to create routes

//Add last
$route['(:any)'] = 'user/$1'; //route any uri not above to the user controller.
[/quote]

have my babies, pickupman. thank you Smile
#7

[eluser]pickupman[/eluser]
LOL..your welcome. Got to admit it picked up reading someone's blog a while back.
#8

[eluser]tkyy[/eluser]
brilliant, this needs a sticky or something since i'm sure tons of people would want to accomplish this in their applications Smile
#9

[eluser]tkyy[/eluser]
hey friends, i am having some bit of problems with this again (sorry to nag), here is what i am using:

Code:
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";

/*
|---------------------------------------------------------------
| ALLOWING DOMAIN.COM/USERNAME (CUSTOM ROUTES)
|---------------------------------------------------------------
|
| Here we will perform a scandir of all of the controllers that
| we have in the project and manually route them. After that,
| we just make a custom route (last) to the user/show_profile
| function. This allows users to have subdirectory-style
| pages that are much easier to share and to remember.
|
*/

$controllers = scandir(absolute().'system/application/controllers/');
        
foreach($controllers as $item){
    if(stristr($item,'.php')){
        $route[str_replace('.php','',$item).'/(:any)'] = str_replace('.php','',$item).'/$1';
    }
}

$route['(:any)'] = 'user/show_profile/$1';

and im getting the dreaded white screen. can someone shed some light?
#10

[eluser]pickupman[/eluser]
Dump your $route array, and see what it contains. Do it with and without the loop and compare what's wrong. You also bump error logging in config.php You see some sort of error in CI log or php log.




Theme © iAndrew 2016 - Forum software by © MyBB