如何使用代码点火器 3.0 在子文件夹中调用控制器


How to call Controller inside sub folder using codeigniter 3.0?

我在代码点火器 3.0 上的控制器子文件夹中创建了控制器文件。

我正在使用查询字符串格式的 URL 而不是段。

我有两种类型的子文件夹,用于后端(管理员)和前端(用户)。

我还在应用程序文件夹的核心文件夹中创建了MY_Router文件。

控制器结构

Controller
--backend
   ---admin.php
   ---product.php
--frontend
   ---user.php

我想要管理面板的网址:

http://localhost/DemoSite/admin_panel/admin/dashboard

admin_panel希望在每次后端控制器调用之前将其包含在 URL 中

管理员是控制器
仪表板是函数

对于前端:

http://localhost/DemoSite/user

我已经完成了这样的路线:

$route['default_controller'] = 'frontend/user';
$route['admin_panel/(:any)'] = "backend/$1";
$route['(:any)'] = "user/$1";

MY_Router文件代码:

<?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';
        }
        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);
        // Assign routed segments, index starting from 1
        $this->uri->rsegments = array(
            1 => $class,
            2 => $method,
        );
        log_message('debug', 'No URI present. Default controller set.');
    }
}

您说您正在使用查询字符串。使用查询字符串时。

更改此内容

$config['uri_protocol'] = 'REQUEST_URI';

对此

$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'QUERY_STRING';

然后启用

$config['enable_query_strings'] = TRUE;
// Controller
$config['controller_trigger'] = 'c';
// Function 
$config['function_trigger'] = 'm';
// Directory
$config['directory_trigger'] = 'd';

如用户指南所示

http://localhost/your_project/index.php?d=admin_panel&c=admin&m=dashboard

例如,仪表板将是管理控制器上的一项功能。

如何将网站网址与查询字符串一起使用

site_url('d=admin_panel&c=admin&m=dashboard');

宽度用户 ID 示例

$id = '1';
site_url('d=admin_panel&c=admin&m=dashboard&user_id=' . $id);

试试这个:

/* for http://localhost/DemoSite/admin_panel/admin/dashboard */
$route['default_controller'] = 'frontend/user';
$route['admin_panel/(:any)/(:any)'] = "backend/$1/$2";
/* For http://localhost/DemoSite/user */
$route['(:any)'] = "frontend/$1";