Welcome Guest, Not a member yet? Register   Sign In
On Multiple Domain and custom view....
#1

[eluser]kimo_gusatava[/eluser]
I always wanted to create a CMS that only had a single core but has multiple domain e.a.: http://domain1.com , http://example.com etc, etc. so I started researching on how to do this with CI till I saw this http://philsturgeon.co.uk/news/2009/06/H...ter-Set-up, But it was out of date (reading through the comment it helped me jump start my plan...)

with this I only edited 1 file
Code:
// lets clean HTTP_HOST;
$clean_host = preg_replace('/www./', '', $_SERVER['HTTP_HOST']); // replace('www.',$_SERVER['HTTP_HOST']);
// remove unnecessary TLD.
/**
* TODO
* this wont remove URL like .co.uk .com.ph ect. will only result on domain.co or domain.com I think I should use preg replace here as well
*/
$ar_host = explode( '.',$clean_host);
array_pop($ar_host);
$host = implode('.', $ar_host);

// add it to the constant the will later be use...
define('SITE', $host);
define('SITE_URL', $_SERVER['HTTP_HOST']);
define('SITE_DIR', FCPATH.'../'.SITE.'/');
/* Location: ./application/config/constants.php */

So that solves one problem on Multiple Domain. next is the templating...
I Need to be able to create a custom view per domain but has a fall back for a default view, Which means I had to extend the
Code:
$this->load->view('welcome_message')
Opps! $_ci_view_paths is PROTECTED in 2.0

anyway thats ok, I won't be using that so I'll just extend the loader:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* MY_Loader Class
*
* Custom Loader class that extends the base CI_Loader to override the location
* of the views directory.
*/
class MY_Loader extends CI_Loader {

    public $_ci_new_view = array();
    
    function __construct() {
        parent::__construct();
        $CI =& get_instance();
        $this->_ci_new_view = array(SITE_DIR.'/views/'=>TRUE);
    }
    public function local_view($view, $vars = array(), $return = FALSE)    {
        if(is_file(SITE_DIR.'/views/'.$view.'.php')) {
            return $this->_ci_new_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
        } else {
            return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
        }
    }
    protected function _ci_new_load($_ci_data)    {
        foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val) {
            $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
        }

        $file_exists = FALSE;

        if ($_ci_path != '') {
            $_ci_x = explode('/', $_ci_path);
            $_ci_file = end($_ci_x);
        } else {
            $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
            $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;

            foreach ($this->_ci_new_view as $view_file => $cascade) {
                if (file_exists($view_file.$_ci_file)) {
                    $_ci_path = $view_file.$_ci_file;
                    $file_exists = TRUE;
                    break;
                }
                if ( ! $cascade) {
                    break;
                }
            }
        }

        if ( ! $file_exists && ! file_exists($_ci_path)) {
            show_error('Unable to load the requested file: '.$_ci_file);
        }


        $_ci_CI =& get_instance();
        foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var) {
            if ( ! isset($this->$_ci_key)) {
                $this->$_ci_key =& $_ci_CI->$_ci_key;
            }
        }

        if (is_array($_ci_vars)) {
            $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
        }
        extract($this->_ci_cached_vars);

        ob_start();

        if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE) {
            echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
        } else {
            include($_ci_path);
        }

        log_message('debug', 'File loaded: '.$_ci_path);

        if ($_ci_return === TRUE) {
            $buffer = ob_get_contents();
            @ob_end_clean();
            return $buffer;
        }

        if (ob_get_level() > $this->_ci_ob_level + 1) {
            ob_end_flush();
        } else {
            $_ci_CI->output->append_output(ob_get_contents());
            @ob_end_clean();
        }
    }
}
#2

[eluser]Bui Duc Long[/eluser]
Great! I'll try your solution Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB