Welcome Guest, Not a member yet? Register   Sign In
Modular extensions with multi sub-directory
#1

[eluser]vee[/eluser]
First, i must give credit to Damien K. http://ellislab.com/forums/viewthread/190563/

This multi sub-directory modification works with CI+Modular extensions.

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

/* load the MX_Router class */
require APPPATH."third_party/MX/Router.php";

class MY_Router extends MX_Router {


protected $module;


function __construct() {
  parent::__construct();
}// __construct


/** Locate the controller **/
public function locate($segments) {  
  
  $this->module = '';
  $this->directory = '';
  $ext = $this->config->item('controller_suffix').EXT;
  
  /* use module route if available */
  if (isset($segments[0]) AND $routes = Modules::parse_routes($segments[0], implode('/', $segments))) {
   $segments = $routes;
  }

  /* get the segments array elements */
  list($module, $directory, $controller) = array_pad($segments, 3, NULL);

  /* check modules */
  foreach (Modules::$locations as $location => $offset) {
  
   /* module exists? */
   if (is_dir($source = $location.$module.'/controllers/')) {
    
    $this->module = $module;
    $this->directory = $offset.$module.'/controllers/';
    
    /* module sub-controller exists? */
    if($directory AND is_file($source.$directory.$ext)) {
     return array_slice($segments, 1);
    }
    
    /* module sub-directory exists? */
    if($directory AND is_dir($source.$directory.'/')) {

     $source = $source.$directory.'/';
     $this->directory .= $directory.'/';

     /* module sub-directory controller exists? */
     if(is_file($source.$directory.$ext)) {
      return array_slice($segments, 1);
     }
    
     /* module sub-directory sub-controller exists? */
     if($controller AND is_file($source.$controller.$ext)) {
      return array_slice($segments, 2);
     }
    }
    
    /* module controller exists? */  
    if(is_file($source.$module.$ext)) {
     return $segments;
    }
   }
  }
  
  /* application controller exists? */  
  if (is_file(APPPATH.'controllers/'.$module.$ext)) {
   return $segments;
  }
  
  /* application sub-directory controller exists? */
  if($directory AND is_file(APPPATH.'controllers/'.$module.'/'.$directory.$ext)) {
   $this->directory = $module.'/';
   return array_slice($segments, 1);
  }
  
  /* ADD */
  /**
   * add multi sub directories support
   * @link http://ellislab.com/forums/viewthread/190563/
   * @author Damien K.
   */
  if ($directory) {
   // @edit: Support multi-level sub-folders
   $dir = '';
   do {
    if (strlen($dir) > 0) {
     $dir .= '/';
    }
    $dir .= $segments[0];
    $segments = array_slice($segments, 1);
   } while (count($segments) > 0 && is_dir(APPPATH . 'controllers/' . $dir . '/' . $segments[0]));
   // Set the directory and remove it from the segment array
   $this->set_directory($dir);
   // @edit: END
   // @edit: If no controller found, use 'default_controller' as defined in 'config/routes.php'
   if (count($segments) > 0 && !file_exists(APPPATH . 'controllers/' . $this->fetch_directory() . $segments[0] . EXT)) {
    array_unshift($segments, $this->default_controller);
   } elseif ( empty($segments) && is_dir( APPPATH.'controllers/'.$this->directory ) ) {
    $segments = array($this->default_controller);
   }
   // @edit: END
   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]);
     // @edit: Fix a "bug" where show_404 is called before all the core classes are loaded
     $this->directory = '';
     // @edit: END
    }
   }
   //print_r($segments);
   if ( $this->directory.$segments[0] == $module.'/'.$this->default_controller ) {
    // skip (for prevent show 404; use next if below)
   } elseif ( count($segments) > 0 && file_exists( APPPATH . 'controllers/' . $this->fetch_directory() . $segments[0] . EXT ) ) {
    return $segments;
   }
  }
  /* end ADD */
  
  /* application sub-directory default controller exists? */
  if (is_file(APPPATH.'controllers/'.$module.'/'.$this->default_controller.$ext)) {
   $this->directory = $module.'/';
   return array($this->default_controller);
  }
}// locate


/* ADD set_directory method. */
function set_directory($dir) {
  $this->directory = str_replace(array('.'), '', $dir) . '/'; // @edit: Preserve '/'
}// set_directory


}
locate in application/core/MY_Router.php

I hope this can help you.
#2

[eluser]Ricardo Vigatti[/eluser]
What's the difference about the original one ?
#3

[eluser]vee[/eluser]
the difference is...

original one cannot create multi sub-directory, only 1 level directory.

this one can create very deep sub-directory.
#4

[eluser]Ricardo Vigatti[/eluser]
[quote author="vee" date="1333606749"]the difference is...

original one cannot create multi sub-directory, only 1 level directory.

this one can create very deep sub-directory.[/quote]

So, that's great. lol.

I have tested before ask you about this, but i don't tried to use MULTI sub-directories, i tried only with one level. I couldn't see the differences. Sorry !




Theme © iAndrew 2016 - Forum software by © MyBB