【问题标题】:How to use a sub folder in default controller route in CodeIgniter 3 [duplicate]如何在 CodeIgniter 3 中使用默认控制器路由中的子文件夹 [重复]
【发布时间】:2016-01-15 09:32:46
【问题描述】:

虽然在没有子目录的情况下设置$route['default_controller'] 它可以工作,但我想在子目录中更改它并且它不工作。

【问题讨论】:

  • 你是怎么去的?你试试我的答案

标签: php codeigniter


【解决方案1】:

默认情况下,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';

【讨论】:

  • 用一个例子很好的解释。
  • 谢谢大佬,回答确实帮助我理解了路由流程,但是在CodeIgniter Version 3.1.0对我来说不起作用,你在3.1.0 version试过了吗
  • 实际上它可以工作...抱歉之前的评论...我的问题是 .htaccess 的错误文件名及其在 CI 3.+ 中的魅力。
  • 干得好,效果很好:)
  • 非常感谢 :) 是的,它与 3.x 版本完美配合
猜你喜欢
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
  • 2016-06-08
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
相关资源
最近更新 更多