CodeIgniter路由问题以访问前端和后端文件夹

时间:2018-06-15 11:20:08

标签: php codeigniter codeigniter-3

我正在使用CodeIgniter。我在控制器和视图中有前端和后端文件夹。我试过服务器步骤甚至检查几乎所有的解决方案,但我仍然无法访问它我的默认控制器

enter image description here

routes.php

$route['default_controller'] = 'frontend/User_control';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

/*backend*********************************/
  $config['backend'] = 'backend/Access_control';  

1)我的问题是当我访问网址http://localhost/example_ci_row/

我找不到404页面

enter image description here

2)如何访问我尝试的后端网址http://localhost/icube_row/admin  但我收到了错误

enter image description here

前端User_control

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_control extends CI_Controller {
    public $current_date;
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('form');
    }
    public function index(){
        $this->load->view('frontend/login');
    }
    }
?>

后端访问控制

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Access_control extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('form');           
    }

    public function index(){
        $this->load->view('backend/login');
    }

    }

?>

被修改

当我使用以下步骤时,它正在工作。我在控制器中添加了Test.php文件并更改了路由,然后我获得了登录页面。

test.php的

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
    public $current_date;
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('form');
    }
    public function index(){
        $this->load->view('frontend/login');
    }
    }
?>

routes.php文件

$route['default_controller'] = 'Test';

2 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助:

内置$route['default_controller']不适用于子文件夹。你必须按照你的要求扩展系统路由器:

您需要在MY_Router.php

中创建application > core > 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.');
    }
}

这将允许您使用$route['default_controller'] = 'frontend/User_control';作为默认控制器

答案 1 :(得分:0)

您是否配置了Base URL? 的应用 - &GT; config-&GT; config.php中

$config['base_url'] = 'http://localhost/example_ci_row';