Sean Fisher
Sean Fisher

Reputation: 625

CI Base URL Routing

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!

Upvotes: 3

Views: 2080

Answers (4)

Bogdan Danila
Bogdan Danila

Reputation: 3

Suppose you have a Controller called "Author", a function in it called "page" which gets as a parameter a username:

class Author extends CI_Controller {

public function page($username = null) {
    if($username == null) { //checking for forced url page load without username specified
        //do a 404 redirect
    } else {
        $this->load->model('m_users');
        if($this->m_users->exists($username)) { // checking if requested username exists

            //do stuff with the user here

        } else { //otherwise redirect
            //do a 404 redirect
        }
    }
}

then I would use the following code at the bottom of config/routes.php to route "your-domain.com/author/page/username" to "your-domain.com/username"

if($handle = opendir(APPPATH.'/controllers')) {
    while(false !== ($controller = readdir($handle))) {
        if($controller != '.' && $controller != '..' && strstr($controller, '.') == '.php') {
            $route[strstr($controller, '.', true)] = strstr($controller, '.', true);
            $route[strstr($controller, '.', true).'/(:any)'] = strstr($controller, '.', true).'/$1';
        }
    }
    closedir($handle);
}
$route['([a-zA-Z0-9_-]+)'] = 'author/page/$1';

this will route any request of the form your-domain.com/whatever to your-domain.com/author/page/whatever only if a Controller with the name "Whatever" doesn't exist. If one exists, then it will access the Controller.

In adition, after all this, if you want to do something like your-domain.com/login to route to your-domain.com/auth/login you can do it by adding the following line to your config/routes.php

//...
$route['login'] = 'auth/login'; //add this line before the one specified above
$route['([a-zA-Z0-9_-]+)'] = 'author/page/$1';

Upvotes: 0

Kieran Andrews
Kieran Andrews

Reputation: 5885

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.

Upvotes: 2

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

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

$route['404_override'] = 'users';

Upvotes: 3

bschaeffer
bschaeffer

Reputation: 2904

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...

Upvotes: 4

Related Questions