2011-03-06 8 views
8

私は、コントローラフォルダ内にコントローラフォルダを設定しています。その中にコントローラがある3つの別個のサブフォルダがあります。CodeIgniter内の複数のサブフォルダへのルーティング

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

私はこの問題を解決するために何をしますか、この

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

などのroutes.phpのファイルにそれをルーティングしようとしましたか?

答えて

11

このコードは、すでにインターネット上だったが、私はそれがここに古いソースを参照してくださいCodeIgniterの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-directories4-5 levelsに問題に直面して(のような/コントローラ/ folder1の/フォルダ2/FOLDER3/folder4 /私のコントローラ)と、それは私の作品

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

から

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]))) 

にwhileループを変更します。

上記のものは2-3 sub-directoriesではOKですが、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]); 
    } 
} 
関連する問題