CodeIgniter Forums

Full Version: How to create our profile url
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]JasmineFlower[/eluser]
Hi,

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.

El Forum

[eluser]Atharva[/eluser]
You can have one additional field in db like seo_username which will store seo friendly username like for eg: jasmine-flower (you can use urltitle()in text helper for this) This seo_username field will be unique, so that you can retrieve the data of that particular user using this field. Eg http://www.example.com/jasmine-flower , then you will use jasmine-flower to fetch the data of that user.

El Forum

[eluser]JasmineFlower[/eluser]
hi,

thank u i already created db for user. In user table user_url field store the usernames.

i need routing concept to view user own profile

In my View:

Code:
<a href="{$base_url}{$user_urlname}" target="_blank" class="item_blog_a">View Profile</a></td>


when i click the above link, it displays the url www.example.com/username .But 404 error raising. so pls tell me the routing or any other way to display user profile in new window

El Forum

[eluser]osci[/eluser]
Code:
$route['(:any)'] = "yourcontroller/$1";

El Forum

[eluser]toopay[/eluser]
@osci,
If you use that route, any request to the server (even it pointed to valid controller) will be re-route to 1 controller(in your ilustration, 'yourcontroller'. It's totally wrong approach!

@Jasmine,
CI routes can't provide your uri spec. But, you can achieve http://www.yoursite/username uri, by make 'pre_system' hooks. Create a hook class, which contain function to inspect whether requested uri is a valid username or not, then intercept SERVER data before it reach codeigniter routes!

El Forum

[eluser]toopay[/eluser]
UPDATE CODE FOR ACCEPTED CONNECTION METHOD, AND USING DATABASE CONFIG

If you dont familiar with hook, here's the way to do that.

1. Enable hook in 'application/config/config.php', find and replace hook section to this...
Code:
$config['enable_hooks'] = TRUE;

2. Configure a 'pre-system' hook in 'application/config/hooks.php'...
Code:
$hook=array(
        'pre_system' => array(
                array(
                         'class'    => 'Userlookup',
                         'function' => 'check_uri',
                         'filename' => 'Userlookup.php',
                         'filepath' => 'hooks',
                         'params'   => NULL,
                      ),
        ),
);
3. You're ready to go. Create a file, called 'Userlookup.php' under 'application/hooks', Then write a class to do your stuff. The prototype, should be something like...
Code:
&lt;?php defined('BASEPATH') or die('No direct script access allowed');

class Userlookup{

    protected $uri_username;
    protected $connection_method;
        
    protected $hostname;
    protected $username;
    protected $password;
    protected $database;

    public function __construct()
    {
        // Configure database connection
        include(APPPATH.'config/database'.EXT);
        $this->hostname = $db[$active_group]['hostname'];
        $this->username = $db[$active_group]['username'];
        $this->password = $db[$active_group]['password'];
        $this->database = $db[$active_group]['database'];
    }

    public function check_uri()
    {
         // First, we need get the uri segment to inspect
         $request_uri = explode('/',substr($_SERVER['REQUEST_URI'], 1));
         $this->uri_username = array_shift($request_uri);
         $connection_router = array_shift($request_uri);
         $this->connection_method = empty($connection_router) ? 'index' : $connection_router;
         // Connect to database, and check the user table
         mysql_connect($this->hostname, $this->username, $this->password) AND mysql_select_db($this->database);
         $res = mysql_query("SELECT user_id FROM user WHERE user_url='".$this->uri_username."'");
         if ($row = mysql_fetch_object($res))
         {
                // If, there is a result, then we should modify server data
                // Below line means, we told CodeIgniter to load
                // 'User' controller on 'index', 'info' or any valid connection/method and we send 'id' parameter
                $_SERVER['REQUEST_URI'] = '/user/'.$this->connection_method.'/'.$row->user_id;
         }
         mysql_free_result($res);
    }
}
4. Now, when you send http://www.yoursite.com/jasmine in your browser, the hook will re-route that to something like http://www.yoursite.com/user/index/1, and for http://www.yoursite.com/jasmine/info the hook will re-route that to http://www.yoursite.com/user/info/1 and so on...and so on...(if 'jasmine' is a valid user and had id '1' in user table).

El Forum

[eluser]osci[/eluser]
@toopay
Quote:@osci,
If you use that route, any request to the server (even it pointed to valid controller) will be re-route to 1 controller(in your ilustration, ‘yourcontroller’. It’s totally wrong approach!

Let's go through this a little and your solution and if I'm wrong pls explain to me.

You say that everything is routed to yourcontroller and that it's totally wrong approach. If you put routes for your other controllers before that route wouldn't that work?

Although I liked your prototype class for refernece since i wasn't familiar with hooks, one thing I didn't like was (as being a prehook) i have to hardcode database settings. and keep track of them for future projects. I guess you could load the database config file and read these values during hook but wouldn't that be too much code (If of course my approach works, otherwise it is a necessity)?

El Forum

[eluser]toopay[/eluser]
[quote author="osci" date="1302522372"]You say that everything is routed to yourcontroller and that it's totally wrong approach. If you put routes for your other controllers before that route wouldn't that work?[/quote]
Of course it work, but... it means you will have to define ALL OF YOUR CONTROLLER above that route! it will break the route concepts at all!

Route concept in CI, was a set (or an array) of regular expression rules which get inspected before system find match controller for server 'request_uri' data. In other word, Route concept enable you to make your uri looks as preety, as shorter, and as logic as possible, when the actual uri didnt give you that option. For example, it enable you to get response from 'www.yoursite/user/userlookup/id' when you access 'www.yoursite/u/id'.

But, as you can see, you need route JUST for specific condition, NOT FOR EVERY CONDITION. Overall route is more like an EXCEPTION. Since it just an EXCEPTION, there is no need to create route for all of your controller, and your idea (unfortunately) is leading into this way.

[quote author="osci" date="1302522372"]i have to hardcode database settings. and keep track of them for future projects. I guess you could load the database config file and read these values during hook but wouldn't that be too much code (If of course my approach works, otherwise it is a necessity)?[/quote]
Because it is a 'pre-system' hook, so it can't load CI resource(like config file, etc). Of course, we can working on it to load resource from config file. But i prefer to keep it independent, it's cleaner and more intuitive(based by my coding standard, of course). Of course, you need to keep track of them for future changes. And in my opinion, its worthed.

El Forum

[eluser]osci[/eluser]
@Jasmine_flower toopay's solution is your solution to your exception, don't consider mine at all.

@toopay thanks for the insight, I guess I needed it. Thanks for the prototype too.

El Forum

[eluser]toopay[/eluser]
@osci.

No problems. You can modify that class, into your ilustration (so you doesn't need to keep track of them for future changes) by use something like "include(APPPATH.'path_to_your_database_config_filename'.EXT)" at contruction function and get the configuration array value.
Pages: 1 2