2

I would like to pass some site-wide validated variables before controller segment in URL.

Example:

Default URL would be:

www.mysite.com/controller/method/variable/

Sometimes I'd like to have also URL like this to refer to user created sub-configuration of this site (theme, menus, ...), so user could share this site URL nicely and others would see the site though his custom configurations.

www.mysite.com/username/controller/method/variable

Here username is custom part of base_url. It should be validated against database and set as session variable to use it later in my controllers and change theme for example. Also all the links on the site would start to use www.mysite.com/username as base_url after website is entered with this username in the URL.

One way to solve this would be routing it like this:

controller/method/variable_name1/variable_value1/user_conf/username

...and the add the implementation to every single controller in my project. But this is not an elegant solution.

2 Answers 2

2

Is this what you're after:

$route['(:any)/(:any)'] = '$2/$1';

where all your function definitions have the username as the last parameter:

class Controller{function page(var1, var2, ..., varn, username){}}

Or, if you only want in on one specific page you could do something like this:

$route['(:any)/controller/page/(:any)'] = 'controller/page/$2/$1'; //This will work for the above class.

Or, if you want it for a number of functions in a controller you could do this:

$route['(:any)/controller/([func1|func2|funcn]+)/(:any)'] = 'controller/$2/$3/$1';
Sign up to request clarification or add additional context in comments.

1 Comment

That's a good explanation about using router class, but it's not exactly what I was looking for, because adding additional input parameter for all the methods in all the controllers would be trouble.
1

After messing with this problem for a day I ended up with adding custom router class to my project. I'm working in CodeIgniter 2.0, so the location of this file should be application/core/MY_Router.php

My code is following:

class MY_Router extends CI_Router {

// --------------------------------------------------------------------

/**
 * OVERRIDE
 *
 * Validates the supplied segments.  Attempts to determine the path to
 * the controller.
 *
 * @access    private
 * @param    array
 * @return    array
 */
function _validate_request($segments)
{
    if (count($segments) == 0)
    {
        return $segments;
    }

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

    $users["username"] = 1;
    $users["minu_pood"] = 2;
    // $users[...] = ...;
    // ...
    // Basically here I load all the 
    // possbile username values from DB, memcache, filesystem, ...
    if (isset($users[$segments[0]])) { 
        // If my segments[0] is in this set
        // then do the session actions or add cookie in my cast.
        setcookie('username_is', $segments[0], time() + (86400 * 7));
        // After that remove this segment so 
        // rounter could search for controller!
        array_shift($segments); 
        return $segments;
    }

    // So segments[0] was not a controller and also not a username...
    // Nothing else to do at this point but show a 404
    show_404($segments[0]);

}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.