views:

762

answers:

3

What should be happening: user navigates to URI, routes.php grabs the State and sends it to the controller, the controller returns some info from a database query. Pretty basic stuff.

The problem: the URI isn't passing the variable to the controller. I'm being told

Missing argument 1 for States::state_summary

I can set a default for the function argument, ie. ($st='Alabama') and everything works smoothly.

I don't even see how this is possible. Maybe at least tell me what I need to test to track down the bug.

URI:

http://example.com/index.php/states/Alabama

routes.php:

$route['states/(.*)'] = "states/state_summary/$1";

States controller:

...

function state_summary($st)
{
// DB query
// Return data
}

...
A: 

I believe your route should be adjusted to this:

$route['states/(:any)'] = "states/state_summary/$1";

That worked for me. I'm not sure if (.*) is valid as I've never seen it used.

Trae
'(.*)' is a valid regular expression.
Tyler Rash
Ah, of course. Well, as I said, the route posted above worked for me. Have you tried it?
Trae
Tyler, where are you placing the route? Maybe that has something to do with it. Per the comments in routes.php"The reserved routes must come before any wildcard or regular expression routes."
Trae
A: 

You got to see this. Thanks :)

Sarfraz
A: 

Well, I never write the controller to have parameter, instead I use rsegment method:

...

function state_summary()
{
  $st = trim($this->uri->rsegment(3));
  // DB query
  // Return data
}
...

With this, I have more control with the passed parameter. I can sanitize it using trim or intval, before pass it to model or library.

Also, there are some tweak in codeigniter core library about routing the url. See it in the file system/libraries/Router.php, the code inside function _parse_routes() around lines 278. It is how URI routing work in CI.

Donny Kurnia