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 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件