CodeIgniter Forums
Can't get routes to work how I want it to - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Can't get routes to work how I want it to (/showthread.php?tid=35580)



Can't get routes to work how I want it to - El Forum - 11-03-2010

[eluser]ElliotReevey[/eluser]
I started work on my first every project using Codeigniter and hoping you guys can help me out.

I have a controller called campaign which has an index() and newcampaign() function which all works fine using the following urls http://website.com/campaign and http://website.com/campaign/newcampaign.

However I am looking to extend this so that anything after http://website.com/campaign which is NOT equal to newcampaign routes to the viewcampaign() function for example http://website.com/campaign/campaign1.

I have so far been working on the routes.php file in the config folder and have tried:

Code:
$route['campaign/^((?!newcampaign)\S*)'] = "campaign/$1";

However when I go to say http://website.com/campaign/campaign1 I get a 404 not found.

Can anyone shed any light on how to achieve what im looking for?

Cheers


Can't get routes to work how I want it to - El Forum - 11-03-2010

[eluser]Bart Mebane[/eluser]
This is one way to do it:
Code:
route['campaign/new'] = 'campaign/newcampaign';
route['campaign/(:any)'] = 'campaign/viewcampaign/$1';
The router will use the first match it finds.


Can't get routes to work how I want it to - El Forum - 11-03-2010

[eluser]ElliotReevey[/eluser]
Code:
$route['campaign/newcampaign'] = 'campaign/newcampaign';
$route['campaign/(:any)'] = 'campaign/viewcampaign';

The above works, however I was hoping to achieve this by using a regular expression which says if NOT equal to newcampaign therefore I wouldn't have to add a new line for every other view that I created from the campaign controller.


Can't get routes to work how I want it to - El Forum - 11-04-2010

[eluser]Bart Mebane[/eluser]
Hmmm ... not sure I'm understanding. If you route all non-newcampaign requests to the same place, wouldn't you still have problems if you later added additional functions? One alternative is to include viewcampaign explicitly in the url (http://website.com/campaign/viewcampaign/campaign1).


Can't get routes to work how I want it to - El Forum - 11-04-2010

[eluser]ElliotReevey[/eluser]
Your right you would, but in the regular expression you could have "(newcampaign|deletecampaign)" or something to that degree rather than having to add new rules for each new page you want to exclude.

I am aware you could add viewcampaign to the URL but thats what I am trying to avoid.


Can't get routes to work how I want it to - El Forum - 11-04-2010

[eluser]Bart Mebane[/eluser]
My preference would be to have a separate route[] line for each function, because it makes things clearer to me, but I understand what you're saying. I've never used regexes in routes, so I'll defer to some of the other forum members.