如何使用codeigniter 3.0在子文件夹内调用控制器?

如何使用codeigniter 3.0在子文件夹内调用控制器?

问题描述:

我已经在codeigniter 3.0上的控制器的子文件夹中创建了控制器文件。如何使用codeigniter 3.0在子文件夹内调用控制器?

我正在使用查询字符串formate的URL不段。

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

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

你昨天问过同样的问题..见:http://*.com/questions/ 35627730/how-to-routes-controller-sub-folder-using-codeigniter#comment58937353_35627730 –

+0

这将帮助你http://*.com/questions/6529026/codeigniter-default-controller-in-a-sub-directory-不工作 – Gopalakrishnan

+0

检查这个http:// *。com/questions/12443275/how-to-call-sub-folder-controller-with-routing-codeigniter?rq = 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'; 

作为上user guide

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); 
+0

感谢给予答案,当从控制器调用函数时,我有一个问题如何重定向到URL。现在我有加载视图和URL是一样的,但我想改变网址或控制器的名称,就像当第一个网址是c = admin_controller&m = index&d = admin后重定向到c = admin_controller&m = dashboard&d = admin –

试试这个:

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

它只调用default_controller路由,其他路由规则不起作用 –

+0

是否使用'.htaccess'删除了index.php。 – Yash

+0

我有访问控制器里面的子文件夹,如果我使用段格式的URL,但是当我启用查询字符串$ config ['enable_query_strings'] = TRUE;它不起作用 –