Welcome Guest, Not a member yet? Register   Sign In
Multi route file
#1

[eluser]guitarlove[/eluser]
Hello,
I trying to make a multi site based on the same code. I done with the parent/child hierachy by making a parent general controller and with the cild a specify the unique fonction for the new site. I redirect to the good controller with the config file by checking the site url. Like this http://philsturgeon.co.uk/blog/2009/06/H...ter-Set-up

But I got a problem and I didn't find a solution. I want to make a routing file per site.

Example,

Code:
Site 1 => site1_route
Site 2 => site2_route

Is this possible to have multiple route file in the config file ?

My second idear was to make a module per new website and including the parent controller, so in fact they use the same code.

thank
#2

[eluser]guitarlove[/eluser]
I got a new idear, but I don't know if it's a "clean" way to do it. I make my own route file MY_Route that extends CI_Router. I just rewrite the function _set_routing, now she take $this->route_file_name.

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



/**
*
* Classe personnalise de CI_Router
*
*
*/
class MY_Router extends CI_Router {


var $route_file_name = '';

/**
  * Constructeur
  */
function MY_Router($_route_file_name){
  parent::__contruct();
  $this->route_file_name = $_route_file_name;
}

/**
  * Permet la gestion de plusieurs fichiers de route situes dans le dossier /config
  * @param string $_route_file_name chemin du fichier route
  */
function _set_routing(){

  // 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->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));

   if (isset($_GET[$this->config->item('function_trigger')]))
   {
    $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
   }
   return;
  }
  
  // Load the routes.php file.
  
  // ** Code original **
  //@include(APPPATH.'config/routes'.EXT);
  
  // -- Ajout choisi la route personnalisee--
  @include(APPPATH.'config/' . $this->route_file_name .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->_fetch_uri_string();
  
  // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
  if ($this->uri->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.");
   }
    
   if (strpos($this->default_controller, '/') !== FALSE)
   {
    $x = explode('/', $this->default_controller);

    $this->set_class(end($x));
    $this->set_method('index');
    $this->_set_request($x);
   }
   else
   {
    $this->set_class($this->default_controller);
    $this->set_method('index');
    $this->_set_request(array($this->default_controller, 'index'));
   }

   // re-index the routed segments array so it starts with 1 rather than 0
   $this->uri->_reindex_segments();
    
   log_message('debug', "No URI present. Default controller set.");
   return;
  }
  unset($this->routes['default_controller']);
  
  // Do we need to remove the URL suffix?
  $this->uri->_remove_url_suffix();

  // Compile the segments into an array
  $this->uri->_explode_segments();
  
  // 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->uri->_reindex_segments();
}

}
#3

[eluser]PhilTem[/eluser]
Why don't you just include multiple routes.php files that are stored in folders according to some slug you gave the pages? o.O
#4

[eluser]guitarlove[/eluser]
[quote author="PhilTem" date="1353085099"]Why don't you just include multiple routes.php files that are stored in folders according to some slug you gave the pages? o.O[/quote]

It will sound stupid (maybe of my bad english), but I don't know how you can include multiple route file ?

Also,my situation updated. For the moment, the codeigniter project, use de modular extension system HMVC.

What I need to do is, for a specific website the good route are loading. Like I said in my first post, for the multiple site, I use a parent/child hierarchy because for the part of the front, i can't use the modular or copy some code. So, i check the url and I want to redirecte to the good route file.

But Now, in this route file, I must take the translation for the url.

Exemple

fr.jesuisunepatate.fr

will be
eng.iamapotato.com

like $route['test'] = _("My string");

I use getText and PO file, but how I can let codeigniter do this ?

thank
#5

[eluser]guitarlove[/eluser]
I found a solution. For people that will have the same problem, if you use HMVC like me, you juste need to add this line
Code:
require 'site_add.php';
in mx/modules.php and change the find fonction with the var
$route_file_name (initialised in site_add.php)



Code:
/** Parse module routes **/
public static function parse_routes($module, $uri) {
  
  /* load the route file */
  if ( ! isset(self::$routes[$module])) {
  
   //------------------AJOUT---------------------
   require 'site_add.php';
   //--------------FIN AJOUT--------------------
  
  
   if (list($path) = self::find($route_file_name, $module, 'config/') AND $path)
    self::$routes[$module] = self::load_file($route_file_name, $path, 'route');
    
  }

I put the code to detect the URL in a another file. It name is site_add like you can see up there.

Code:
switch ($_SERVER['HTTP_HOST']) {

case 'www.site1.com':
  $route_file_name = 'routes_1';
  break;

case 'www.site2..com':
  $route_file_name = 'routes_2';
  break;

default:
  $route_file_name = 'routes';
  break;

}





Theme © iAndrew 2016 - Forum software by © MyBB