繁体   English   中英

如何使用Codeigniter 3.0在子文件夹中调用Controller?

[英]How to call Controller inside sub folder using codeigniter 3.0?

我已经在codeigniter 3.0的controller的子文件夹中创建了controller文件。

我正在使用查询字符串formate作为url而不是segment。

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

我还在应用程序文件夹的核心文件夹中创建了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.');
    }
}

尝试这个:

/* 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";

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

改变这个

$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 = '1';
site_url('d=admin_panel&c=admin&m=dashboard&user_id=' . $id);

暂无
暂无

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

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