Part of the EllisLab Network
   
 
Callbacks in Routing
Posted: 07 November 2008 04:10 PM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  200
Joined  12-31-2007

I want to allow dashes in the URI, so I must manually add a route for each URL that I want.
Example:
http://www.abc.com/growing-seeds/
is $route[‘growing-seeds’] = “growingseeds”;

My goal is to set up a regular expression to do this automatically because setting
up a route for every possible page is not an option.

This works for a path that only contains 1 dash.
Example:
http://www.abc.com/growing-seeds/
http://www.abc.com/deep-roots/
$route[’([A-Za-z0-9]+)-([A-Za-z0-9]+)’] = strtolower(”$1$3”);

My problem:
I want to be able to allow any number of dashes.
Example:
http://www.abc.com/growing-seeds-from-dirt/
$route[‘growing-seeds-from-dirt’] = “growingseedsfromdirt”;

How do I set this up automatically?

I’ve tried this, but it doesn’t seem to work:
$route[’([^/]+)’] = str_replace(’-’, ‘’, “$1”);


Any suggestions?

Profile
 
 
Posted: 07 November 2008 04:14 PM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1089
Joined  05-17-2008

Do you have a corresponding controller for every page?
e.g. is there a controller “growingseeds”, “growingseedsfromdirt” and so on?

Profile
 
 
Posted: 07 November 2008 04:18 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  200
Joined  12-31-2007

Yes there will be.

Basically, my goal is to reduce the number of steps to create a page to 2 instead of 3.

I’m fine with having to create a view and controller for each page.
I don’t want to add a route for every page though. It needs to be automatic.

Edit:
Just so you know, I plan on changing the logic of loading the controllers slightly, but I want to know how to do this first…

Profile
 
 
Posted: 07 November 2008 04:26 PM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1089
Joined  05-17-2008

Are you using apache? If yes, you could try to use mod_rewrite, so you would not need any custom routes.

Profile
 
 
Posted: 07 November 2008 04:49 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  200
Joined  12-31-2007

Yes, I’ve thought about that, but I’m not sure how to actually replace the dashes with nothing in order to get the controller name. (in .htaccess)

Is it not possible to apply a callback to a match such as $1 or $2?

I don’t understand why I can’t (simply) do
$route[’([^/]+)’] = str_replace(’-’, ‘’, ‘$1’);

strtolower() seems to work… why wouldnt str_replace()?

Profile
 
 
Posted: 07 November 2008 06:02 PM   [ Ignore ]   [ # 5 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  114
Joined  09-09-2008
simshaun - 07 November 2008 09:49 PM

strtolower() seems to work… why wouldnt str_replace()?

the function are getting called as soon as the routes.php is included
by the router-class.
not when the route gets applied.


you could extend the router-class and add the behavior you need “by hand”. wink

Profile
 
 
Posted: 07 November 2008 06:05 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  200
Joined  12-31-2007

Yea. I’ve come to realize that (in another thread.)

The problem is, I probably won’t remember I did it when I upgrade CodeIgniter later. smile

I’ve posted to the Feature request for something to be added.

It seems I’m only one of many who want the dashes.

Profile