簡體   English   中英

使用CodeIgniter在子文件夾中路由控制器

[英]Routing controllers in subfolders using CodeIgniter

因為我想分離系統的前端和后端。 我在控制器中創建了2個文件夾作為前端和后端

下面是我的控制器文件夾的結構

controller
 --frontend
   ---store.php
   ---processing.php
   ---profile.php
   ---authenticate.php
   ---register.php 

 --backend
   ---authenticate.php
   ---stats.php
   ---users.php
   ---property_manage.php
   ---register.php 

我可以通過使用來訪問這些功能

frontend/store/add
frontend/store/manage
......

backend/stats/sales
backend/stats/payments
.....

但我想從網址中取下前端和后端段。

我檢查了codeigniter中的路由功能,但據我所知,我需要單獨指定每條路由。 由於我有大約12個控制器,每個控制器有大約10-15個功能,我可能需要指定路由的每個功能。

有沒有其他有效的方法來實現使用路由或任何其他方式? (不使用任何htaccess)

做這個:

$route['store/(:any)'] = 'frontend/store/$1';
$route['processing/(:any)'] = 'frontend/processing/$1';
$route['profile/(:any)'] = 'frontend/profile/$1';

后端相同:

$route['backend/(:any)'] = 'backend/authenticate/$1';

您不必在routes.php中為控制器的每個功能創建每個規則,而是如上所述,每個控制器一個規則就足夠了。

URI路由:CodeIgniter用戶指南

$1代表第一個表達式,這里(:any)是表達式,你可以在每個規則上有多個表達式,表達式在另一邊表示為$1$2等等。

類似地, (:num)將匹配僅包含數字的段, (:any)將匹配包含任何字符的段, (\\d+)將匹配任何數字, ([az]+)將匹配任何alpha文本。

你必須能夠以某種方式區分前端和后端。 也許設置一條路線,將任何帶有“admin”的uri轉發到后端,以及任何沒有“admin”的前端。

對於前端,您可以在routes.php中添加:

$this->set_directory( "frontend" );

所以在瀏覽器URL中,不需要包含“前端”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM