2011-03-06 25 views
8

我有一个管理员文件夹设置在我的控制器目录下,在那里我有3个独立的子文件夹,里面有控制器。CodeIgniter中的多个子文件夹的路由

-- Controllers 
---- Admin 
------ Dashboard 
-------- dashboard.php 
-------- file.php 
------ Members 
-------- members.php 
-------- file.php 
------ Settings 
-------- settings.php 
-------- file.php 

我试图路由,在routes.php文件文件中像这样

$route['admin/(:any)/(:any)'] = 'admin/$1/$2'; 
$route['admin/(:any)'] = 'admin/$1/$1'; 
$route['admin'] = 'admin/index'; 

我该怎么办,以解决这一问题?

回答

11

此代码已经在互联网上,但我修改了它,使之成为笨2.1

看到工作的旧的源位置: http://glennpratama.wordpress.com/2009/10/20/multi-level-subfolder-for-controller-in-codeigniter/

创建一个新的文件MY_Router.php在应用程序/核心目录中,复制下面的代码:

<?php 

/* 
* Custom router function v 0.2 
* 
* Add functionality : read into more than one sub-folder 
* 
*/ 

Class MY_Router extends CI_Router 
{ 
    Function MY_Router() 
    { 
     parent::__construct(); 
    } 

    function _validate_request($segments) 
    { 
     if (file_exists(APPPATH.'controllers/'.$segments[0].EXT)) 
     { 
      return $segments; 
     } 

     if (is_dir(APPPATH.'controllers/'.$segments[0])) 
     { 
      $this->set_directory($segments[0]); 
      $segments = array_slice($segments, 1); 

      /* ----------- ADDED CODE ------------ */ 

      while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0])) 
      { 
       // Set the directory and remove it from the segment array 
      //$this->set_directory($this->directory . $segments[0]); 
      if (substr($this->directory, -1, 1) == '/') 
       $this->directory = $this->directory . $segments[0]; 
      else 
       $this->directory = $this->directory . '/' . $segments[0]; 

      $segments = array_slice($segments, 1); 
      } 

      if (substr($this->directory, -1, 1) != '/') 
       $this->directory = $this->directory . '/'; 

      /* ----------- END ------------ */ 

      if (count($segments) > 0) 
      { 

       if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().'/'.$segments[0].EXT)) 
       { 
        show_404($this->fetch_directory().$segments[0]); 
       } 
      } 
      else 
      { 
       $this->set_class($this->default_controller); 
       $this->set_method('index'); 

       if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().'/' .$this->default_controller.EXT)) 
       { 
        $this->directory = ''; 
        return array(); 
       } 

      } 

      return $segments; 
     } 

     show_404($segments[0]); 
    } 
} 
+0

这是jondavidjohn接受的答案中链接到的代码的更新变体。 –

+0

谢谢!像魅力一样工作:-) –

+0

将无法​​与Codeigniter 3 – user4419336

1

我是与sub-directories(如/控制器/资料夹/文件夹2/folder3/folder4 /我的控制器)的4-5 levels

while(count($segments) > 0 && 
    // checks only $this->directory having a/
    is_dir(APPPATH.'controllers/'.$this->directory.$segments[0])) 

面临的问题和改变while循环

while(count($segments) > 0 && 
    // check $this->directory having a/
    (is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]) || 
     // check $this->directory not having/
     is_dir(APPPATH.'controllers/'.$this->directory.'/'.$segments[0]))) 

这对我的作品。

上面的一个对于2-3 sub-directories是可以的,但对于4-5 sub-directory层次结构不适用。

3

对于Codeigniter 3.x兼容性:自从删除对PHP 4的支持后,EXT常量的用法已被弃用。不再需要维护不同的文件扩展名,并且在这个新的CodeIgniter版本(3.x)中, EXT常量已被删除。只需使用'.php'。

所以新MY_Router.php:

<?php 

/* 
* Custom router function v 0.3 
* 
* Add functionality : read into more than one sub-folder 
* Compatible with Codeigniter 3.x 
* 
*/ 

Class MY_Router extends CI_Router 
{ 
    Function MY_Router() 
    { 
     parent::__construct(); 
    } 

    function _validate_request($segments) 
    { 

     if (file_exists(APPPATH.'controllers/'.$segments[0].".php")) 
     { 
      return $segments; 
     } 

     if (is_dir(APPPATH.'controllers/'.$segments[0])) 
     { 
      $this->set_directory($segments[0]); 
      $segments = array_slice($segments, 1); 

      /* ----------- ADDED CODE ------------ */ 

      while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0])) 
      { 
       // Set the directory and remove it from the segment array 
      //$this->set_directory($this->directory . $segments[0]); 
      if (substr($this->directory, -1, 1) == '/') 
       $this->directory = $this->directory . $segments[0]; 
      else 
       $this->directory = $this->directory . '/' . $segments[0]; 

      $segments = array_slice($segments, 1); 
      } 

      if (substr($this->directory, -1, 1) != '/') 
       $this->directory = $this->directory . '/'; 

      /* ----------- END ------------ */ 

      if (count($segments) > 0) 
      { 

       if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().'/'.$segments[0].".php")) 
       { 
        show_404($this->fetch_directory().$segments[0]); 
       } 
      } 
      else 
      { 
       $this->set_class($this->default_controller); 
       $this->set_method('index'); 

       if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().'/' .$this->default_controller.".php")) 
       { 
        $this->directory = ''; 
        return array(); 
       } 

      } 

      return $segments; 
     } 

     show_404($segments[0]); 
    } 
}