2011-03-06 21 views
8

में एकाधिक उप फ़ोल्डर्स का मार्ग मेरे पास मेरे नियंत्रक निर्देशिका में स्थापित एक व्यवस्थापक फ़ोल्डर है, जिसके तहत मेरे पास 3 अलग-अलग सब-फ़ोल्डर्स हैं जिनमें से नियंत्रक हैं।कोडइग्निटर

-- 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 फ़ाइल में मार्ग की कोशिश की?

उत्तर

4

"आउट ऑफ़ द बॉक्स" कोडनिर्देशक आपके नियंत्रक निर्देशिका में एकाधिक उपनिर्देशिका स्तरों का समर्थन नहीं करता है, केवल एक।

There is a way to extend the routing class to support this, check this blog entry.

+2

यह अभी भी 3 संस्करण में मामला है? – GavinR

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

कोडिनेटर 3 – user4419336

1

मैं था

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

से sub-directories (जैसे/नियंत्रक/फ़ोल्डर 1/folder2/folder3/folder4/मेरी नियंत्रक) की 4-5 levels के साथ समस्या का सामना करना पड़ और बदल जबकि पाश

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

कोडनिर्देशक 3.x संगतता के लिए: PHP 4 के लिए समर्थन छोड़ने के बाद EXT निरंतर का उपयोग बहिष्कृत कर दिया गया है। अब अलग-अलग फ़ाइल नाम एक्सटेंशन को बनाए रखने की आवश्यकता नहीं है और इस नए कोडइग्निटर संस्करण (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]); 
    } 
}