Welcome Guest, Not a member yet? Register   Sign In
User Profile URL
#1

[eluser]~Chris~[/eluser]
any tips on how i can make user URL's

for example

http://www.example.com/username

will display a dynamic page containing the user's profile.
#2

[eluser]~Chris~[/eluser]
well, im looking at what i wrote and realized it is very vague. I can handle all the dynamic page and everything. what i basically need to know is how to set the controller to know if its a username in the url and not to try and call a controller by that name.

so that when u go to www.example.com/username it will not throw a 404. and if you go to www.example.com/realcontroller it will not try and load the user profile controller, but load the real controller.
#3

[eluser]m4rw3r[/eluser]
Why not use a controller named user (and route it to user/display/[username])? Then you won't have this problem.

Use the routes.php config file to route the user to the right controller function (http://ellislab.com/codeigniter/user-gui...uting.html).
#4

[eluser]vadivelan[/eluser]
I have yet to complete reading CI, still I believe the following approach seems good.

If there is no controller associated with a particular request then a 404 is thrown and request finally reach the application/error/error_404.php

Here we can verify how many segments that actual request carry. If the total segment is only one, collect that segment using uri->segment(1) and check against your users ( db / collection , array ) and proceed further.

But the issue is, I don't know how to use the CI's libraries ( uri - to count the segement and any other libraries to do some process ) in application/error/error_404.php template. Because we cannot access CI resources outside controller, I tried with $CI = &getInstance;();, but without success.

If we know how to use CI's libraries outside controller especially in /application/error/error_404, I think this is done.
#5

[eluser]~Chris~[/eluser]
yes, i was thinking upon the same line as you at first vadivelan. tapping into the error page, but i thought about the same issues too. I dont want to use a user controller because i want to have as short a url as possible. I was thinking about using htaccess but it would just interfere with the codignitor mod rewrites. I was also thinking about modding the core to handle whether it was an actual 404 or a user uri, but I do not really want to do that, because it is unclean. maybe if there was a good hook to use or something.
#6

[eluser]vadivelan[/eluser]
Second approach is to use a hook.
1. Create a pre-controller hook
2. From that hook access the total number of segments ( using uri library ).
3. If total segment is one, collect the uri_path (using uri's uri_string )
4. Check if this related to any real path using file's set_realpath. If this doesn't resolve to valid path then you shall verify and generate your user profile or redirect to their profile page.

if( $this->uri->total_rsegments() == 1 ) {
$uristring = $this->uri->uri_string();
if( ! $this->file->set_realpath('./system/application/'.$uristring . ".php" ) {
echo(" got it");
}
}

But like my above post, I don't know how to access CI's libraries within hook. lol.
#7

[eluser]vadivelan[/eluser]
I agree with you Chris, these are not clean approaches. These approaches will break in a production environment over the period of time and not maintainable.
#8

[eluser]~Chris~[/eluser]
I found this. I may be able to use its concept.

http://ellislab.com/forums/viewthread/70848/
#9

[eluser]Michael Wales[/eluser]
Just establish a route for all of the URLs you do know - for a fact. Then setup an open route to handle anything else (your users).

Code:
$routes['user/register'] = 'user/register';
$routes['user/activate'] = 'user/activate';
$routes['widgets/buy'] = 'widgets/buy';
$routes['about'] = 'home/about';
$routes['(:any)'] = "user/info/$1";
#10

[eluser]~Chris~[/eluser]
ah yes michael wales, thank you very much. I was making it much more complicated than i needed to.
p.s. Thanks for Erkana auth.




Theme © iAndrew 2016 - Forum software by © MyBB