doushih06137 2016-02-25 07:06
浏览 184
已采纳

如何使用codeigniter 3在子文件夹中路由控制器?

I have create an controller in subfolder of controller folder and in subfolder i create an login controller.

But i couldn't access this controller and already given rule in route file.

Structure :

Controller
--admin
   ---login.php

Route Rule :

$route['default_controller'] = 'admin/login';
$route['admin_login'] = 'admin/login';
  • 写回答

2条回答 默认 最新

  • dourun2990 2016-02-25 07:15
    关注

    By default codeIgniter 3 versions and up you can not use sub folders in your default controller route.

    To be able to use a sub folder in default_controller you need to use a MY_Router.php

    $route['default_controller'] = 'admin/login';
    

    application >

    application > core >

    application > core > MY_Router.php

    MY_Router.php

    <?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.');
        }
    }
    

    Also make sure your you follow the codeIgniter way of naming files

    File name: Login.php

    <?php 
    
    class Login extends CI_Controller {
        public function index() {
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?