CodeIgniter allows the controller to support the Router class library of multiple levels of subdirectories

Source: Internet
Author: User

My_router.php put in the System/application/libraries directory, you can let CI controller support multilevel subdirectory.
In this way, you can place more levels of directory in the System/application/controllers directory, Access is index.php/directory 1/Directory 2/Directory 3/Controller/method/Parameters
Please note that you do not need load, because this class is automatically load by the system.

my_router.php Code

<?phpif( !defined(' BasePath '))Exit(' No Direct script access allowed ');/** * Custom Routing class * * Let CI controller support multilevel directory * * @author sohocn.net * @copyright copyright©2012-2018 www.sohocn.net all Righ TS reserved. * @created 2012-12-13 * @updated 2012-12-13 * @version 1.0*/ classMy_routerextendsci_router{/** Set The directory name * * @access public * @param string * @return void*/    functionSet_directory ($dir)    {        $this->directory =$dir.‘ /‘; }     /** * Validates the supplied segments.     Attempts to determine the path to * the controller. * * @access Private * @param array * @return Array*/     function_validate_request ($segments)    {        if(Count($segments) = = 0)        {            return $segments; }         //Does The requested controller exist in the root folder?        if(file_exists(APPPATH. ' Controllers/'.$segments[0]. '. Php))        {            return $segments; }         //is the controller in a sub-folder?        if(Is_dir(APPPATH. ' Controllers/'.$segments[0]))        {            $temp=Array(' dir ' = =Array(), ' path ' = = APPPATH. ' controllers/'); foreach($segments  as $k=$v)            {                $temp[' path ']. =$v.‘ /‘; if(Is_dir($temp[' Path ']))                {                    $temp[' dir '] [] =$v; unset($segments[$k]); }            }             $this->set_directory (implode(‘/‘,$temp[' dir '])); $segments=array_values($segments); unset($temp); if(Count($segments) > 0)            {                //Does The requested controller exist in the sub-folder?                if( !file_exists(APPPATH. ' Controllers/'.$this->fetch_directory ().$segments[0]. '. Php))                {                    if( !Empty($this->routes[' 404_override ']))                    {                        $x=Explode(‘/‘,$this->routes[' 404_override ']); $this->set_directory (' '); $this->set_class ($x[0]); $this->set_method (isset($x[1])?$x[1]: ' Index '); return $x; }                    Else{show_404 ($this->fetch_directory ().$segments[0]); }                }            }            Else            {                //Is the method being specified in the route?                if(Strpos($this->default_controller, '/')!==FALSE)                {                    $x=Explode(‘/‘,$this-Default_controller); $this->set_class ($x[0]); $this->set_method ($x[1]); }                Else                {                    $this->set_class ($this-Default_controller); $this->set_method (' Index '); }                 //Does The default controller exist in the sub-folder?                if( !file_exists(APPPATH. ' Controllers/'.$this->fetch_directory ().$this->default_controller. '. Php))                {                    $this->directory = ' '; return Array(); }             }             return $segments; }          //If we ' ve gotten this far it means, the URI does not correlate to a valid//controller class. We 'll now see if there are an override        if( !Empty($this->routes[' 404_override ']))        {            $x=Explode(‘/‘,$this->routes[' 404_override ']); $this->set_class ($x[0]); $this->set_method (isset($x[1])?$x[1]: ' Index '); return $x; }          //Nothing else to does at this point but show a 404show_404 ($segments[0]); }}//END my_router Class 

CodeIgniter allows the controller to support the Router class library of multiple levels of subdirectories

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.