Why isn't CodeIgniter passing variables? [duplicate]
Possible Duplicate:
CodeIgniter Routing
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开发者_如何学C 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
}
...
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.
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.
精彩评论