├── README ├── license └── core └── MY_Router.php /README: -------------------------------------------------------------------------------- 1 | Multi level controllers extension - Production ready - CI 2.1.0 compatible 2 | ==================================================================== 3 | 4 | An extension to the core router class to allow controllers to be in multi-level directories. 5 | 6 | Notes 7 | ----- 8 | 9 | This original extension was written by Damien K. but the code appeared to have no home. 10 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2010 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /core/MY_Router.php: -------------------------------------------------------------------------------- 1 | 0) 49 | { 50 | $dir .= '/'; 51 | } 52 | 53 | $dir .= $segments[0]; 54 | $segments = array_slice($segments, 1); 55 | } while (count($segments) > 0 && is_dir(APPPATH.'controllers/'.$dir.'/'.$segments[0])); 56 | 57 | // Set the directory and remove it from the segment array 58 | $this->set_directory($dir); 59 | // @edit: END 60 | 61 | // @edit: If no controller found, use 'default_controller' as defined in 'config/routes.php' 62 | if (count($segments) > 0 && ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT)) 63 | { 64 | array_unshift($segments, $this->default_controller); 65 | } 66 | // @edit: END 67 | 68 | if (count($segments) > 0) 69 | { 70 | // Does the requested controller exist in the sub-folder? 71 | if (!file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT)) 72 | { 73 | // show_404($this->fetch_directory().$segments[0]); 74 | // @edit: Fix a "bug" where show_404 is called before all the core classes are loaded 75 | $this->directory = ''; 76 | // @edit: END 77 | } 78 | } 79 | else 80 | { 81 | // Is the method being specified in the route? 82 | if (strpos($this->default_controller, '/') !== FALSE) 83 | { 84 | $x = explode('/', $this->default_controller); 85 | 86 | $this->set_class($x[0]); 87 | $this->set_method($x[1]); 88 | } 89 | else 90 | { 91 | $this->set_class($this->default_controller); 92 | $this->set_method('index'); 93 | } 94 | 95 | // Does the default controller exist in the sub-folder? 96 | if (!file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) 97 | { 98 | $this->directory = ''; 99 | return array(); 100 | } 101 | 102 | } 103 | 104 | return $segments; 105 | } 106 | 107 | // If we've gotten this far it means that the URI does not correlate to a valid 108 | // controller class. We will now see if there is an override 109 | if (!empty($this->routes['404_override'])) 110 | { 111 | $x = explode('/', $this->routes['404_override']); 112 | 113 | $this->set_class($x[0]); 114 | $this->set_method(isset($x[1]) ? $x[1] : 'index'); 115 | 116 | return $x; 117 | } 118 | 119 | // Nothing else to do at this point but show a 404 120 | show_404($segments[0]); 121 | } 122 | 123 | // -------------------------------------------------------------------- 124 | 125 | /** 126 | * OVERRIDE 127 | * 128 | * Set the directory name 129 | * 130 | * @access public 131 | * @param string 132 | * @return void 133 | */ 134 | function set_directory($dir) 135 | { 136 | $this->directory = str_replace(array('.'), '', $dir).'/'; // @edit: Preserve '/' 137 | } 138 | 139 | // -------------------------------------------------------------------- 140 | } 141 | --------------------------------------------------------------------------------