如何在CodeIgniter 3中使用默认控制器路由中的子文件夹

时间:2016-01-15 09:32:46

标签: php codeigniter

在将$route['default_controller']设置为子目录时,它正在工作,但我想在子目录中更改它并且它不起作用。

2 个答案:

答案 0 :(得分:13)

默认情况下,codeigniter 3您将无法在default_route

中拥有子文件夹

您需要使用MY_Router.php

位于应用程序>核心> MY_Router.php

这将使您能够在CI3中的default_controller中使用子文件夹

<?php

class MY_Router extends CI_Router {
    protected function _set_default_controller() {

        if (empty($this->default_controller)) {

            show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
        }
        // Is the method being specified?
        if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
            $method = 'index';
        }

        // This is what I added, checks if the class is a directory
        if( is_dir(APPPATH.'controllers/'.$class) ) {

            // Set the class as the directory

            $this->set_directory($class);

            // $method is the class

            $class = $method;

            // Re check for slash if method has been set

            if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
                $method = 'index';
            }
        }

        if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {

            // This will trigger 404 later

            return;
        }
        $this->set_class($class);
        $this->set_method($method);
        // Assign routed segments, index starting from 1
        $this->uri->rsegments = array(
            1 => $class,
            2 => $method
        );
        log_message('debug', 'No URI present. Default controller set.');
    }
}

然后这应该让你能够做到

$route['default_controller'] = 'subfolder/controller/function';

答案 1 :(得分:-1)

CodeIgniter内置$ route [&#39; default_controller&#39;]不适用于项目中的子文件夹。

如果要为子文件夹设置默认路由,则应执行以下操作:

$route['sub_folder'] = "sub_folder/default_contoller";

这样做,你对CI说的是,如果有人去路线:

http://localhost/sub_folder/ 

就像

http://localhost/sub_folder/default_controller