Welcome Guest, Not a member yet? Register   Sign In
Passing parameters to index()
#1

[eluser]alexdemers[/eluser]
Hey, really new to the community and Code Igniter. To date, Code Igniter is really awesome. I have been able to do _almost_ what I what except for this. I want to be able to pass in parameters to my index() method of my controller but it seems to not work when I do not type in explicitly "index/" in the address bar.

This works: /items/index/category:3/brand:4/
This is not working: /items/category:3/brand:4/

I get a 404 Page Not Found error when trying to access the second URI.

In my index() method, I use func_get_args to get the arguments, so the real declaration is:

Code:
function index()
{

}

The basic functionality for this is the Items controller gets all the items in a table and the parameters are actually filters.

Am I doing something wrong or this is not the approach I should go for?

Thanks!
#2

[eluser]Ben Edmunds[/eluser]
Not sure if this is the best way but if you are extending the Controller class with MY_Controller in the construct you could do an is_method() and if its not assume its a variable...

Or you could probably do something in the routing for this as well but I'm not too well versed on the routing...
#3

[eluser]louis w[/eluser]
This will not work how your wanting to. The first argument is always treated as the method name. I would suggest looking into _remap if you want it exactly like this.
#4

[eluser]alexdemers[/eluser]
Well, I do have other methods also. So the _remap won't work. I guess what I can do is to do another method especially for filtering. Any other thoughts?
#5

[eluser]Ben Edmunds[/eluser]
You also might be better off just using something other than index. My index() method is usually pretty slim while view() gets used a lot.

[edit]You beat me to it, haha[/edit]
#6

[eluser]louis w[/eluser]
Then you can't expect to remove index from the url like you want and be able to pass textual values like that. It's going to think its a method.
#7

[eluser]BrianDHall[/eluser]
OK, if you really want it to work like that you will need to use a route in your routes.php file and use a regex to grab "category:" and pass it to the function you want it to go to.

It always goes mysite.com/controller/function/parameters unless you define it otherwise. If you go /controller/somethingelse it will go through the routes and attempt to find the somethingelse() function in your controller, and if it isn't there it will 404 you.

Routes provide limited redirection elss extreme than _remap.

I'd rig up the routes regex rule for you, but regex makes my brain hurt and I avoid it unless I am required to use it Smile
#8

[eluser]Colin Williams[/eluser]
You can use _remap() and still leave other methods accessible. Use your noggin. It's pretty straight-forward.




Theme © iAndrew 2016 - Forum software by © MyBB