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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13955335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:20:42  来源:igfitidea点击:

Routing controllers in subfolders using CodeIgniter

phpcodeignitercodeigniter-2

提问by LiveEn

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

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

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
.....

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

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

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.

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

is there any other efficient way to achieve using routing or any other way? (without using any htaccess)

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

回答by hsuk

Do this:

做这个:

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

Same for backend :

后端相同:

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

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.

您不必为控制器的每个功能在 routes.php 中创建每个规则,而是每个控制器一个规则就足够了,如上所述。

URI Routing : CodeIgniter User Guide

URI 路由:CodeIgniter 用户指南

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

$1表示第一个表达式,这里(:any)是表达式,你可以在每个规则上有多个表达式,表达式表示为$1$2以此类推。

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.

类似地,(:num)将匹配仅包含数字的段,(:any)将匹配包含任何字符的段,(\d+)将匹配任何数字,([a-z]+)将匹配任何字母文本。

回答by swatkins

You have to be able to differentiate the frontend from the backend somehow. Maybe set a route that forwards any uri with "admin" to the backend, and anything without "admin" to the frontend.

您必须能够以某种方式区分前端和后端。也许设置一个路由,将任何带有“admin”的uri转发到后端,将没有“admin”的任何uri转发到前端。

回答by Vishnu Ugale

For Front-End you can add this in routes.php:

对于前端,您可以在 routes.php 中添加:

$this->set_directory( "frontend" );

so in browser URL, there is no need to include "frontend"

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