简体   繁体   English

子文件夹中的默认控制器codeigniter 3无法正常工作

[英]default controller inside subfolder codeigniter 3 not working

In codeigniter 3 application i have directory structure like this: 在codeigniter 3应用程序中我有这样的目录结构:

-Myproject
  -application
    -controllers
     -home
       Welcome.php   //This is my controller inside home directory

How to set Welcome controller as default controller? 如何将Welcome控制器设置为默认控制器? I use below code 我使用下面的代码

$route['default_controller'] = 'home/Welcome';

This routing works for previous versions of codeigniter. 此路由适用于以前版本的codeigniter。

By default, you are not allowed to do that. 默认情况下,您不允许这样做。 To get around this, you need to hack your system Router.php : 要解决这个问题,你需要破解你的系统Router.php

codeigniter/system/core/Router.php 笨/系统/核心/ Router.php

Edit a few lines of code so that it becomes like this: 编辑几行代码,使其如下所示:

笨/系统/ router.php

line 1. if (!sscanf($this->default_controller, '%[^/]/%[^/]/%s', $directory, $class, $method) !== 2) 第1行if (!sscanf($this->default_controller, '%[^/]/%[^/]/%s', $directory, $class, $method) !== 2)

line 2. if ( ! file_exists(APPPATH.'controllers'. DIRECTORY_SEPARATOR . $directory. DIRECTORY_SEPARATOR .ucfirst($class).'.php')) 第2行if ( ! file_exists(APPPATH.'controllers'. DIRECTORY_SEPARATOR . $directory. DIRECTORY_SEPARATOR .ucfirst($class).'.php'))

line 3. $this->set_directory($directory); 第3行。 $this->set_directory($directory);

Once you've done, you can call the default controller under directory. 完成后,您可以调用目录下的默认控制器。

$route['default_controller'] = 'home/Welcome'; $ route ['default_controller'] ='主页/欢迎';

You need not to change anything from files inside CODEIGNITER system folder. 您无需从CODEIGNITER系统文件夹中的文件更改任何内容。 Codeigniter allows developer to extend their feature. Codeigniter允许开发人员扩展其功能。 You can create a file named as 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.');
        }
        if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
            $method = 'index';
        }
        if( is_dir(APPPATH.'controllers/'.$class) ) {
            $this->set_directory($class);
            $class = $method;
            if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
                $method = 'index';
            }
        }

        if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
            return;
        }
        $this->set_class($class);
        $this->set_method($method);
        $this->uri->rsegments = array(
            1 => $class,
            2 => $method
        );
        log_message('debug', 'No URI present. Default controller set.');
    }
}

Note: Do not change the file name. 注意:请勿更改文件名。

Try This in routes.php 在routes.php中尝试这个

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM