views:

87

answers:

3

For example, in Twitter you can have this URL format: http://twitter.com/username/

With "username" being the username for the user.

I am wondering on the proper method to have that in Codeigniter. I would need the same format. I have other pages such as user account management, about, etc. Would I need to route it through one function, check if that user exists and then pass it onto another controller? Thanks!

+1  A: 

It would be easy to do something like this:

http://twitter.com/u/username

You just create the controller called "U"

class U extends Controller{

    function index($username){
        echo $username;
    }
}

If you want it at the base url then something else like routing etc will need to be done. Someone else might have this CI know how.

Kieran Andrews
+2  A: 

Extend the Router Class by placing a MY_Router.php in your application\libraries directory and use this code:

<?php 

class MY_Router extends CI_Router {

    function _validate_request($segments)
    {
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {       
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        // **
        // THIS IS THE NEW CODE BELOW
        // **
        // It forces the segments to your known class (user) & method (index)
        // for all controller calls that don't exist as files or inside
        // directories

        $my_segments = array('user', 'index', $segments[0]);    

        return $my_segments;
    }
}

Now, just create a User controller with an index method that accepts username as the first parameter:

<?php

class User extends Controller {

    function index($username = '')
    {
        // Validate the HECK out of $username
        // Validate the HECK out of $username
        // VALIDATE THE HECK OUT OF $username
        echo $username;
        exit();
    }

}

That's a ballin' answer! Tested on CI 1.7.2. Don't know about 2.0, though...

bschaeffer
lol @ `// VALIDATE THE HECK OUT OF $username`
BoltClock
+1  A: 

In CI 2.0 you can do this without any hacks, just add the route:

$route['404_override'] = 'users';
Phil Sturgeon
Does that handle all `show_404()` calls?
bschaeffer
Well once CI 2.0 is released, I'll use that.
Sean Fisher
Use 2.0 now, it's fine. Remember ExpressionEngine, MojoMotor and PyroCMS are all using it so it can't be that unstable.
Phil Sturgeon