Welcome Guest, Not a member yet? Register   Sign In
prepending vars (useful tip when working with subdomains or multilanguage sites)
#1

[eluser]smoku[/eluser]
Hello!

I moded CI a little bit because I needed to prepend var.

With this mod you can create links like:
http://www.domain.com/index.php?user=USE...er/method/....

I needed this in order to create user sites with subdomains:
USERNAME.domain.com/controller/method => domain.com/index.php?user=USERNAME&/controller/method/

But it could be also very useful with multilanguage sites.
You can easily redirect:
domain.com/LANG/controller/method => domain.com/index.php?lang=LANG&/controller/method

query_strings must be TURNED ON!

----------------------------

Here is the code:

Open Router.php

In function _set_route_mapping() at the begining

change:
Code:
// Fetch the complete URI string
$this->uri_string = $this->_get_uri_string();

to:
Code:
// Fetch the complete URI string
$this->uri_string = $this->_remove_query_strings($this->_get_uri_string());

Add this function to router.php:
Code:
function _remove_query_strings($string)
{
    $start = strpos($string, "/");
    if ($start == 0)
    {
       return $string;
    }
    else
    {
        $end = strlen($string);
        $length = $end-$start;
        return substr($string, $start, $length);
    }
}

I don't know if anyone finds this helpful but I needed this in my project so I'm posting this, because I couldn't find it...
#2

[eluser]wiredesignz[/eluser]
Add this to MY_router.php and put it in your application/libraries so you don't break the CI core
Extend class MY_router off CI_Router.

Code:
class MY_router extends CI_Router
{
    //your original function overrides go here.
    
    //your new special functions go here too.
}
#3

[eluser]smoku[/eluser]
[quote author="wiredesignz" date="1201248547"]Add this to MY_router.php and put it in your application/libraries so you don't break the CI core
Extend class MY_router off CI_Router.
[/quote]

I know, this is just a example how to do this, everyone will include this the way they like.
#4

[eluser]Craig A Rodway[/eluser]
Those URLs look awful. Surely there's a better way.
#5

[eluser]wiredesignz[/eluser]
Quote:everyone will include this the way they like.

There is only one recommended way to extend/alter the CI core, unless you never plan to upgrade CodeIgniter.
#6

[eluser]smoku[/eluser]
[quote author="Craig Rodway" date="1201276777"]Those URLs look awful. Surely there's a better way.[/quote]

You rewrite those URLs using mod_rewrite, the way you did it when you wrote apps without framework.

If there is a better way, tell me how to make user's sites:

USERNAME.domain.com/controller/method/...

(wildcard *.domain.com => domain.com)


MY_Router.php:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {

    function MY_Router()
    {
        parent::CI_Router();
    }
    
    function _set_route_mapping()
    {    
            
        // Are query strings enabled in the config file?
        // If so, we're done since segment based URIs are not used with query strings.
        if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
        {
            $this->set_class(trim($this->_filter_uri($_GET[$this->config->item('controller_trigger')])));

            if (isset($_GET[$this->config->item('function_trigger')]))
            {
                $this->set_method(trim($this->_filter_uri($_GET[$this->config->item('function_trigger')])));
            }
            
            return;
        }
                
        
        // Load the routes.php file.
        @include(APPPATH.'config/routes'.EXT);
        $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
        unset($route);

        // Set the default controller so we can display it in the event
        // the URI doesn't correlated to a valid controller.
        $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);    
        
        // Fetch the complete URI string
        $this->uri_string = $this->_remove_query_strings($this->_get_uri_string());

        // If the URI contains only a slash we'll kill it
        if ($this->uri_string == '/')
        {
            $this->uri_string = '';
        }
    
        // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
        if ($this->uri_string == '')
        {
            if ($this->default_controller === FALSE)
            {
                show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
            }
        
            $this->set_class($this->default_controller);
            $this->set_method('index');

            log_message('debug', "No URI present. Default controller set.");
            return;
        }
        unset($this->routes['default_controller']);
        
        // Do we need to remove the suffix specified in the config file?
        if  ($this->config->item('url_suffix') != "")
        {
            $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);
        }
        
        // Explode the URI Segments. The individual segments will
        // be stored in the $this->segments array.    
        foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
        {
            // Filter segments for security
            $val = trim($this->_filter_uri($val));
        
            if ($val != '')
                $this->segments[] = $val;
        }
        
        // Parse any custom routing that may exist
        $this->_parse_routes();        
        
        // Re-index the segment array so that it starts with 1 rather than 0
        $this->_reindex_segments();
    }
    

    function _remove_query_strings($string)
    {
            $start = strpos($string, "/");
            if ($start == 0)
            {
                return $string;
            }
            else
            {
                $end = strlen($string);
                $length = $end-$start;
                return substr($string, $start, $length);
            }
    }

}
?>




Theme © iAndrew 2016 - Forum software by © MyBB