Welcome Guest, Not a member yet? Register   Sign In
404 on reserved route page
#1

[eluser]new_igniter[/eluser]
Hello,
I am building profile pages for a site I am working on an using the "reserved routes" method (show in the code below) so that I can present a url like sitedomain.com/username

Code:
$url_parts        = explode('/',$_SERVER['REQUEST_URI']);
$reserved_routes  = array("splash","user");

if (!in_array($url_parts[1], $reserved_routes)) {
   $route['([a-zA-Z0-9_-]+)'] = "user/index/$1";
}

The code above works great and get no errors.

My problem
I want to be pass in an optional second parameter on the user/index function, so that I have username/parameter but when I add this parameter this I get a 404. I am guessing I need to map the parameter, but would like to know the best way to do this.

My controller function for "user" looks like this:
Code:
function user($userName,$type=null) {

}

Any help is greatly appreciated.
#2

[eluser]beaufrusetta[/eluser]
Is there any particular reason you are passing in "$type"? What is that for? Can you look that type up with the "$userName" var?
#3

[eluser]new_igniter[/eluser]
well, the user has different types of feeds, so I need to be able to say "username/home" to get the "home" feed or username/friends to get the "friends" feed. That is why Ii am using $type.
#4

[eluser]beaufrusetta[/eluser]
Why don't you just setup special routes:

## Dashboard
$route['user/(:any)'] = "user/$1";
## Friends
$route['user/(:any)/friends'] = "user/friends/$1";
## Settings
$route['user/(:any)/settings'] = "user/settings/$1";

This assumes a "User" controller with 3 functions: index, friends, settings. The var $1 is the user name to pass in to those functions.

Does that help?
#5

[eluser]new_igniter[/eluser]
yes, that's great, thanks!
#6

[eluser]beaufrusetta[/eluser]
NP - let me know if you have any other questions.
#7

[eluser]new_igniter[/eluser]
Hey, one last question, how do I reserve controller names so they do not get interpreted as usernames?



Code:
$url_parts        = explode('/',$_SERVER['REQUEST_URI']);
$reserved_routes  = array("splash","user","consume");

if (!in_array($url_parts[1], $reserved_routes)) {

        $route['user/(:any)'] = "user/index/$1";
        ## Friends
        $route['user/(:any)/friends'] = "user/friends/$1";

}
#8

[eluser]beaufrusetta[/eluser]
Define them in the routes section ABOVE the section I wrote out. They will get picked up first and will not be processed. If the segment doesn't match the routes specified there, it will fall through to the next in the list in the config/routes.php file.
#9

[eluser]new_igniter[/eluser]
cool. I get it, thanks for your help. Much appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB