21

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)

jww
  • 97,681
  • 90
  • 411
  • 885
LiveEn
  • 3,193
  • 12
  • 59
  • 104

3 Answers3

26

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.

always-a-learner
  • 3,671
  • 10
  • 41
  • 81
hsuk
  • 6,770
  • 13
  • 50
  • 80
  • thanks mate... works great.. so i need in define a route for each controller. can u please tell me what does $1 represent? – LiveEn Dec 19 '12 at 15:32
  • 1
    $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 – hsuk Dec 19 '12 at 15:37
  • 2
    (: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 – hsuk Dec 19 '12 at 15:39
  • 1
    This will not work if the controllers have the same name (e.g. from his own structure: register.php and processing.php). – rzb Jan 02 '15 at 00:21
  • @rzb Do you have any working solution for that case? – Sam Tigle Jan 10 '20 at 16:40
2

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

$this->set_directory( "frontend" );

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

Gerard Rozsavolgyi
  • 4,834
  • 4
  • 32
  • 39
1

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.

swatkins
  • 13,530
  • 4
  • 46
  • 78