[1] | 1 | <?php (defined('BASEPATH')) OR exit('No direct script access allowed'); |
---|
| 2 | |
---|
| 3 | /* load the MX core module class */ |
---|
| 4 | require dirname(__FILE__).'/Modules.php'; |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * Modular Extensions - HMVC |
---|
| 8 | * |
---|
| 9 | * Adapted from the CodeIgniter Core Classes |
---|
| 10 | * @link http://codeigniter.com |
---|
| 11 | * |
---|
| 12 | * Description: |
---|
| 13 | * This library extends the CodeIgniter router class. |
---|
| 14 | * |
---|
| 15 | * Install this file as application/third_party/MX/Router.php |
---|
| 16 | * |
---|
| 17 | * @copyright Copyright (c) 2011 Wiredesignz |
---|
| 18 | * @version 5.4 |
---|
| 19 | * |
---|
| 20 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 21 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 22 | * in the Software without restriction, including without limitation the rights |
---|
| 23 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 24 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 25 | * furnished to do so, subject to the following conditions: |
---|
| 26 | * |
---|
| 27 | * The above copyright notice and this permission notice shall be included in |
---|
| 28 | * all copies or substantial portions of the Software. |
---|
| 29 | * |
---|
| 30 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 31 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 32 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 33 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 34 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 35 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 36 | * THE SOFTWARE. |
---|
| 37 | **/ |
---|
| 38 | class MX_Router extends CI_Router |
---|
| 39 | { |
---|
| 40 | protected $module; |
---|
| 41 | |
---|
| 42 | public function fetch_module() { |
---|
| 43 | return $this->module; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | public function _validate_request($segments) { |
---|
| 47 | |
---|
| 48 | if (count($segments) == 0) return $segments; |
---|
| 49 | |
---|
| 50 | /* locate module controller */ |
---|
| 51 | if ($located = $this->locate($segments)) return $located; |
---|
| 52 | |
---|
| 53 | /* use a default 404_override controller */ |
---|
| 54 | if (isset($this->routes['404_override']) AND $this->routes['404_override']) { |
---|
| 55 | $segments = explode('/', $this->routes['404_override']); |
---|
| 56 | if ($located = $this->locate($segments)) return $located; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /* no controller found */ |
---|
| 60 | show_404(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** Locate the controller **/ |
---|
| 64 | public function locate($segments) { |
---|
| 65 | |
---|
| 66 | $this->module = ''; |
---|
| 67 | $this->directory = ''; |
---|
| 68 | $ext = $this->config->item('controller_suffix').EXT; |
---|
| 69 | |
---|
| 70 | /* use module route if available */ |
---|
| 71 | if (isset($segments[0]) AND $routes = Modules::parse_routes($segments[0], implode('/', $segments))) { |
---|
| 72 | $segments = $routes; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /* get the segments array elements */ |
---|
| 76 | list($module, $directory, $controller) = array_pad($segments, 3, NULL); |
---|
| 77 | |
---|
| 78 | /* check modules */ |
---|
| 79 | foreach (Modules::$locations as $location => $offset) { |
---|
| 80 | |
---|
| 81 | /* module exists? */ |
---|
| 82 | if (is_dir($source = $location.$module.'/controllers/')) { |
---|
| 83 | |
---|
| 84 | $this->module = $module; |
---|
| 85 | $this->directory = $offset.$module.'/controllers/'; |
---|
| 86 | |
---|
| 87 | /* module sub-controller exists? */ |
---|
| 88 | if($directory AND is_file($source.$directory.$ext)) { |
---|
| 89 | return array_slice($segments, 1); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /* module sub-directory exists? */ |
---|
| 93 | if($directory AND is_dir($source.$directory.'/')) { |
---|
| 94 | |
---|
| 95 | $source = $source.$directory.'/'; |
---|
| 96 | $this->directory .= $directory.'/'; |
---|
| 97 | |
---|
| 98 | /* module sub-directory controller exists? */ |
---|
| 99 | if(is_file($source.$directory.$ext)) { |
---|
| 100 | return array_slice($segments, 1); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /* module sub-directory sub-controller exists? */ |
---|
| 104 | if($controller AND is_file($source.$controller.$ext)) { |
---|
| 105 | return array_slice($segments, 2); |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /* module controller exists? */ |
---|
| 110 | if(is_file($source.$module.$ext)) { |
---|
| 111 | return $segments; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /* application controller exists? */ |
---|
| 117 | if (is_file(APPPATH.'controllers/'.$module.$ext)) { |
---|
| 118 | return $segments; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /* application sub-directory controller exists? */ |
---|
| 122 | if($directory AND is_file(APPPATH.'controllers/'.$module.'/'.$directory.$ext)) { |
---|
| 123 | $this->directory = $module.'/'; |
---|
| 124 | return array_slice($segments, 1); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /* application sub-directory default controller exists? */ |
---|
| 128 | if (is_file(APPPATH.'controllers/'.$module.'/'.$this->default_controller.$ext)) { |
---|
| 129 | $this->directory = $module.'/'; |
---|
| 130 | return array($this->default_controller); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | public function set_class($class) { |
---|
| 135 | $this->class = $class.$this->config->item('controller_suffix'); |
---|
| 136 | } |
---|
| 137 | } |
---|