2016-02-26 71 views
-1

我已經在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://stackoverflow.com/questions/ 35627730/how-to-routes-controller-sub-folder-using-codeigniter#comment58937353_35627730 –

+0

這將幫助你http://stackoverflow.com/questions/6529026/codeigniter-default-controller-in-a-sub-directory-不工作 – Gopalakrishnan

+0

檢查這個http:// stackoverflow。com/questions/12443275/how-to-call-sub-folder-controller-with-routing-codeigniter?rq = 1 –

回答

0

你說你正在使用查詢字符串。使用查詢字符串時。

更改此

$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 –

1

試試這個:

/* 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;它不起作用 –

相關問題