Welcome Guest, Not a member yet? Register   Sign In
Callbacks in Routing
#1

[eluser]simshaun[/eluser]
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?
#2

[eluser]pistolPete[/eluser]
Do you have a corresponding controller for every page?
e.g. is there a controller "growingseeds", "growingseedsfromdirt" and so on?
#3

[eluser]simshaun[/eluser]
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...
#4

[eluser]pistolPete[/eluser]
Are you using apache? If yes, you could try to use mod_rewrite, so you would not need any custom routes.
#5

[eluser]simshaun[/eluser]
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()?
#6

[eluser]narkaT[/eluser]
[quote author="simshaun" date="1226112565"]strtolower() seems to work... why wouldnt str_replace()?[/quote]

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

[eluser]simshaun[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB