Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

php - Routing controllers in subfolders using CodeIgniter

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

but i want to take off the frontend and the backend segments from the 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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do this:

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

Same for backend :

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

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 Routing : CodeIgniter User Guide

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...