Welcome Guest, Not a member yet? Register   Sign In
Wildcard subdomain points to controllers
#6

[eluser]chrisnharvey[/eluser]
Hello,

Sorry for reopening such an old topic, but I stumbled upon this post when searching Google for the same result and thought I would post my complete solution that was never fully resolved in this post. So just in case anyone may search for it in the future, I will post the solution.

To add the subdomain segment to the beginning of the segments array, simply use "array_shift" and "array_unshift" before returning the segments.

Array_shift is used to remove the default controller from the beginning of the segments array if you are accessing the root of the subdomain (e.g. user.example.com).

Array_unshift is used to add the subdomain onto the beginning of the segments array.

Like this:
Code:
{
     if (file_exists(APPPATH.'controllers/'.$http[0].EXT))
     {
          if($segments[0] == $this->default_controller) // Check if default controller and remove
          {
               array_shift($segments);
          }
          array_unshift($segments, $http[0]);
          return $segments;
     }
}

So the full MY_Router subclass will now be:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Router Class
*
* Extends CI Router
*
* @author     Original by EllisLab - extension by CleverIgniters
* @see        http://codeigniter.com
*/

class MY_Router extends CI_Router {
    
    /**
     * Constructor
     *
     * @access    public
     */
    function MY_Router()
    {
        parent::CI_Router();
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Validate Routing Request
     *
     * @access    public
     */
    function _validate_request($segments)
    {
        
        // CHECK TO SEE IF SUBDOMAIN AND GO ON FROM THERE
        $http = explode('.', $_SERVER['HTTP_HOST']);
        if(count($http) > 2)
        {
            if($http[0] != 'www')
            {
                if(is_dir(APPPATH.'controllers/'.$http[0]))
                {
                    // Set the directory and remove it from the segment array
                    $this->set_directory($http[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;
                }
                else
                {
                    if (file_exists(APPPATH.'controllers/'.$http[0].EXT))
                    {
                         if($segments[0] == $this->default_controller)
                         {
                              array_shift($segments);
                         }
                         array_unshift($segments, $http[0]);
                         return $segments;
                    }
                }
            }
        }
        //END SUBDOMAIN LOGIC HERE
        
        // 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;
        }
        
        // Can't find the requested controller...
        show_404($segments[0]);
    }
}
?>

Hope this helps Smile

- Chris


Messages In This Thread
Wildcard subdomain points to controllers - by El Forum - 01-12-2009, 12:32 AM
Wildcard subdomain points to controllers - by El Forum - 01-12-2009, 02:18 AM
Wildcard subdomain points to controllers - by El Forum - 01-12-2009, 02:44 AM
Wildcard subdomain points to controllers - by El Forum - 01-12-2009, 03:03 AM
Wildcard subdomain points to controllers - by El Forum - 01-15-2009, 04:26 PM
Wildcard subdomain points to controllers - by El Forum - 03-17-2011, 11:24 AM



Theme © iAndrew 2016 - Forum software by © MyBB