Welcome Guest, Not a member yet? Register   Sign In
Problem: Routing Using Wild Cards
#1

[eluser]Shiju S S[/eluser]
I have a route defined as follows
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "frontarea/frontpage";
$route['404_override'] = '';
$route['home'] = "frontarea/frontpage";

// Admin Area
$route['([admin][a-z_]+)'] = 'adminarea/$1';
$route['([admin][a-z_]+)/(:any)'] = 'adminarea/$1/$2';
$route['([admin][a-z_]+)/(:any)/(:any)'] = 'adminarea/$1/$2/$3';
$route['([admin][a-z_]+)/(:any)/(:any)/(:any)'] = 'adminarea/$1/$2/$3/$4';


// Franchise Area

$route['([franchisee]+[a-z_]+)'] = 'franchiseearea/$1';
$route['([franchisee]+[a-z_]+)/(:any)'] = 'franchiseearea/$1/$2';
$route['([franchisee]+[a-z_]+)/(:any)/(:any)'] = 'franchiseearea/$1/$2/3';
$route['([franchisee]+[a-z_]+)/(:any)/(:any)/(:any)'] = 'franchiseearea/$1/$2/$3/$4';

// front Area
$route['([front][a-z_]+)'] = 'frontarea/$1';
$route['([front][a-z_]+)/(:any)'] = 'frontarea/$1/$2';
$route['([front][a-z_]+)/(:any)/(:any)'] = 'frontarea/$1/$2/3';
$route['([front][a-z_]+)/(:any)/(:any)/(:any)'] = 'frontarea/$1/$2/$3/$4';

// staff Area
$route['([staff]+[a-z_]+)'] = 'staffarea/$1';
$route['([staff][a-z_]+)/(:any)'] = 'staffarea/$1/$2';
$route['([staff][a-z_]+)/(:any)/(:any)'] = 'staffarea/$1/$2/3';
$route['([staff][a-z_]+)/(:any)/(:any)/(:any)'] = 'staffarea/$1/$2/$3/$4';

URLs like
Quote:adminreports/printreport/1
franchiseereports/printreport/1
is working correctlyis working correctly

but
Quote:frontpage/printreport/1
staffreport/printreport/1
is not working

When I placed the staff part in the top it is working !
What I could under stand is that only the first 2 sections is routing correctly. After that I am getting a
404 Page not found error.
#2

[eluser]InsiteFX[/eluser]
Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
#3

[eluser]Shiju S S[/eluser]
Logically:
staffreport/printreport/1 should match

Code:
// staff Area
$route['([staff]+[a-z_]+)'] = 'staffarea/$1';
$route['([staff][a-z_]+)/(:any)'] = 'staffarea/$1/$2';
$route['([staff][a-z_]+)/(:any)/(:any)'] = 'staffarea/$1/$2/3';
$route['([staff][a-z_]+)/(:any)/(:any)/(:any)'] = 'staffarea/$1/$2/$3/$4';

But it is showing a 404. But works well when placed above the admin area defenition. Why is it so?
#4

[eluser]CroNiX[/eluser]
Along the lines of what InsiteFX said, you need to reverse all of your routes so the ones covering the most segments comes before those with fewer.

Like the post you did just before this one. Reverse them all and same with the others.
Code:
// staff Area
$route['([staff][a-z_]+)/(:any)/(:any)/(:any)'] = 'staffarea/$1/$2/$3/$4';
$route['([staff][a-z_]+)/(:any)/(:any)'] = 'staffarea/$1/$2/3';
$route['([staff][a-z_]+)/(:any)'] = 'staffarea/$1/$2';
$route['([staff]+[a-z_]+)'] = 'staffarea/$1';

Also, some of your regex differs slightly with an extra + (or missing one). Check the last line of the code I posted vs the others. There is a + between [staff] and [a-z_]. Your other rules dealing with other than staff have some missing/added as well.
#5

[eluser]Shiju S S[/eluser]
I have redefined the routes as
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "frontarea/frontpage";
$route['404_override'] = '';
$route['home'] = "frontarea/frontpage";

// Admin Area
$route['([admin]+[a-z_]+)/(:any)/(:any)/(:any)'] = 'adminarea/$1/$2/$3/$4';
$route['([admin]+[a-z_]+)/(:any)/(:any)'] = 'adminarea/$1/$2/$3';
$route['([admin]+[a-z_]+)/(:any)'] = 'adminarea/$1/$2';
$route['([admin]+[a-z_]+)'] = 'adminarea/$1';

// Franchise Area
$route['([franchisee]+[a-z_]+)/(:any)/(:any)/(:any)'] = 'franchiseearea/$1/$2/$3/$4';
$route['([franchisee]+[a-z_]+)/(:any)/(:any)'] = 'franchiseearea/$1/$2/3';
$route['([franchisee]+[a-z_]+)/(:any)'] = 'franchiseearea/$1/$2';
$route['([franchisee]+[a-z_]+)'] = 'franchiseearea/$1';

// staff Area
$route['([staff]+[a-z_]+)/(:any)/(:any)/(:any)'] = 'staffarea/$1/$2/$3/$4';
$route['([staff]+[a-z_]+)/(:any)/(:any)'] = 'staffarea/$1/$2/3';
$route['([staff]+[a-z_]+)/(:any)'] = 'staffarea/$1/$2';
$route['([staff]+[a-z_]+)'] = 'staffarea/$1';

// front Area
$route['([front]+[a-z_]+)/(:any)/(:any)/(:any)'] = 'frontarea/$1/$2/$3/$4';
$route['([front]+[a-z_]+)/(:any)/(:any)'] = 'frontarea/$1/$2/3';
$route['([front]+[a-z_]+)/(:any)'] = 'frontarea/$1/$2';
$route['([front]+[a-z_]+)'] = 'frontarea/$1';
Still the problem persists.
The controllers in admin area and franchiseearea is working well.
Staff area and front area is showing 404.

The reason why I am getting upset is, when I changed the order to:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "frontarea/frontpage";
$route['404_override'] = '';
$route['home'] = "frontarea/frontpage";

// front Area
$route['([front]+[a-z_]+)/(:any)/(:any)/(:any)'] = 'frontarea/$1/$2/$3/$4';
$route['([front]+[a-z_]+)/(:any)/(:any)'] = 'frontarea/$1/$2/3';
$route['([front]+[a-z_]+)/(:any)'] = 'frontarea/$1/$2';
$route['([front]+[a-z_]+)'] = 'frontarea/$1';

// staff Area
$route['([staff]+[a-z_]+)/(:any)/(:any)/(:any)'] = 'staffarea/$1/$2/$3/$4';
$route['([staff]+[a-z_]+)/(:any)/(:any)'] = 'staffarea/$1/$2/3';
$route['([staff]+[a-z_]+)/(:any)'] = 'staffarea/$1/$2';
$route['([staff]+[a-z_]+)'] = 'staffarea/$1';


// Admin Area
$route['([admin]+[a-z_]+)/(:any)/(:any)/(:any)'] = 'adminarea/$1/$2/$3/$4';
$route['([admin]+[a-z_]+)/(:any)/(:any)'] = 'adminarea/$1/$2/$3';
$route['([admin]+[a-z_]+)/(:any)'] = 'adminarea/$1/$2';
$route['([admin]+[a-z_]+)'] = 'adminarea/$1';

// Franchise Area
$route['([franchisee]+[a-z_]+)/(:any)/(:any)/(:any)'] = 'franchiseearea/$1/$2/$3/$4';
$route['([franchisee]+[a-z_]+)/(:any)/(:any)'] = 'franchiseearea/$1/$2/3';
$route['([franchisee]+[a-z_]+)/(:any)'] = 'franchiseearea/$1/$2';
$route['([franchisee]+[a-z_]+)'] = 'franchiseearea/$1';
Front and Staff is working. Now admin and franchisee is not working.
#6

[eluser]CroNiX[/eluser]
Regex isn't my strongest suite, especially in how CI processes them in routes in conjunction with it's own processing, but I've seen others who have used regex have problems with it not working as expected in CI. You might try putting some debug code in Router.php (or extend it with MY_Router) and see how CI is interpreting the regex and what it's doing with it. It might be that the regex itself is fine (and works as expected outside of CI) but the problem is introduced when CI executes it on top of it's own things it is normally doing to process the custom route.

Or maybe someone with more regex experience sees a problem with it.
#7

[eluser]Shiju S S[/eluser]
What I could figure out is that the router is stopping at

$route['([franchisee]+[a-z_]+)'] = 'franchiseearea/$1';

The last route in the second set. (Franchisee area)

ie:
When I give a URL
staffreport/showcontact
It is redirecting to
/franchiseearea/staff

All routes defined below:
$route['([franchisee]+[a-z_]+)'] = 'franchiseearea/$1';
is getting directed to it.
Any solution. I am also trying to gather more information.
#8

[eluser]Shiju S S[/eluser]
The URI
frontdocuments/showcontact
is matching in preg_match() inside router.php in 3 places

([franchisee]+[a-z_]+)/(.+) franchiseearea/frontdocuments/showcontact
([staff]+[a-z_]+)/(.+) staffarea/frontdocuments/showcontact
([front]+[a-z_]+)/(.+) frontarea/frontdocuments/showcontact


and as usual the first one is taken which is causing the error.
Is there any RegEx experts who can help me.

#9

[eluser]InsiteFX[/eluser]
You can find a RegX tester here and even download it, see bottom right of page.

RegX Tester
#10

[eluser]Shiju S S[/eluser]
The problem was in RegEx as explained by Sr. Research Associate CroNiX.

I got it corrected with the following route:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "frontarea/frontpage";
$route['404_override'] = '';
$route['home'] = "frontarea/frontpage";

// Admin Area
$route['(^admin+:any)/(:any)/(:any)/(:any)'] = 'adminarea/$1/$2/$3/$4';
$route['(^admin+:any)/(:any)/(:any)'] = 'adminarea/$1/$2/$3';
$route['(^admin+:any)/(:any)'] = 'adminarea/$1/$2';
$route['(^admin+:any)'] = 'adminarea/$1';
$route['(^admin+)'] = 'adminarea/$1';

// Franchise Area
$route['(^franchisee+:any)/(:any)/(:any)/(:any)'] = 'franchiseearea/$1/$2/$3/$4';
$route['(^franchisee+:any)/(:any)/(:any)'] = 'franchiseearea/$1/$2/3';
$route['(^franchisee+:any)/(:any)'] = 'franchiseearea/$1/$2';
$route['(^franchisee+:any)'] = 'franchiseearea/$1';
$route['(^franchisee+)'] = 'franchiseearea/$1';

// staff Area
$route['(^staff+:any)/(:any)/(:any)/(:any)'] = 'staffarea/$1/$2/$3/$4';
$route['(^staff+:any)/(:any)/(:any)'] = 'staffarea/$1/$2/3';
$route['(^staff+:any)/(:any)'] = 'staffarea/$1/$2';
$route['(^staff+:any)'] = 'staffarea/$1';
$route['(^staff+)'] = 'staffarea/$1';

// front Area
$route['(^front+:any)/(:any)/(:any)/(:any)'] = 'frontarea/$1/$2/$3/$4';
$route['(^front+:any)/(:any)/(:any)'] = 'frontarea/$1/$2/3';
$route['(^front+:any)/(:any)'] = 'frontarea/$1/$2';
$route['(^front+:any)'] = 'frontarea/$1';
$route['(^front+)'] = 'frontarea/$1';

The [] around the name caused the problem. eg: [admin]+

Thanks for the support.




Theme © iAndrew 2016 - Forum software by © MyBB