使用 CodeIgniter 在子文件夹中路由控制器 [英] Routing controllers in subfolders using CodeIgniter

查看:37
本文介绍了使用 CodeIgniter 在子文件夹中路由控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我想把系统的前端和后端分开.我在控制器中创建了 2 个文件夹作为前端和后端

since i want to separate the frontend and backend of the system. i have created 2 folders inside controllers as frontend and backend

下面是我的控制器文件夹的结构

Below is the structure of my controller folder

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

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

我可以通过使用来访问这些功能

i can access the functions by using

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

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

但我想从 url 中删除前端和后端段.

but i want to take off the frontend and the backend segments from the url.

我检查了 codeigniter 中的路由功能,但据我所知,我需要单独指定每条路由.由于我有大约 12 个控制器,每个控制器有大约 10 -15 个函数,因此我可能需要为路由指定每个函数.

I checked the routing feature in codeigniter but according to my knowledge i need to individually specify each route. Since i have about 12 controllers and each has around 10 -15 functions i might have to specify each and every function to the route.

有没有其他有效的方法可以使用路由或任何其他方式来实现?(不使用任何htaccess)

is there any other efficient way to achieve using routing or any other way? (without using any 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 中为控制器的每个功能创建每个规则,如上所述,每个控制器一个规则就足够了.

You don't have to create each rule in routes.php for every function of the controller, rather one rule per controller will be enough as mentioned above.

URI 路由:CodeIgniter 用户指南

$1 表示第一个表达式,这里(:any) 是表达式,每个规则可以有多个表达式,表达式表示为 $1$2 等等.

$1 represent the first expression, here (:any) is the expression, you can have multiple expression on each rule, and expression is represented as $1, $2 and so on on the other side.

同理,(:num) 会匹配一个只包含数字的段,(:any) 会匹配一个包含任意字符的段,(d+) 将匹配任何数字,([az]+) 将匹配任何字母文本.

Similarly, (:num) will match a segment containing only numbers, (:any) will match a segment containing any character, (d+) will match any digit, ([a-z]+) will match any alpha text.

这篇关于使用 CodeIgniter 在子文件夹中路由控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆