Welcome Guest, Not a member yet? Register   Sign In
URI segments, Controllers parameters, which is the right way to do it?
#1

[eluser]AzizLight[/eluser]
Hi everybody,

Since now, I've always used $this->uri->segment() to refer to uri segments. But then I saw that a lot of people used controllers parameters instead, I looked it up in the doc and it's done that way too.

So my question is: which is the right way to do it, and what is the difference between the two considering that the first method worked for me so far?
#2

[eluser]bretticus[/eluser]
[quote author="AzizLight" date="1253862470"]Hi everybody,

Since now, I've always used $this->uri->segment() to refer to uri segments. But then I saw that a lot of people used controllers parameters instead, I looked it up in the doc and it's done that way too.

So my question is: which is the right way to do it, and what is the difference between the two considering that the first method worked for me so far?[/quote]

Not much really. Just your preference. The URI class is automatically loaded so you don't even have to think about loading it. Unused function/method parameters are handled as optional so even that is not a big deal.

I have only used CI for about 1 year now. I typically (almost always) use the URI class, so I haven't experienced any possible caveats with function params. However, again, it seems that the framework does a nice job of making either method problem free.
#3

[eluser]AzizLight[/eluser]
Good to know. Thanks for your answer Smile
#4

[eluser]jedd[/eluser]
Parameters on a controller's methods, you mean? That's got my vote.

You're unlikely to start using sub-directories for your controllers, and shifting things around, but if you do so, it becomes marginally easier than if you're using uri segment calls.

I think the conventional method also makes the code much more readable - that first line of the method declaration is where people typically go first in order to glean insight about the function's parameters. I think it's also a much neater way of setting defaults (otherwise it's a ternary operator, at best, elsewhere to test / set default values).
#5

[eluser]BrianDHall[/eluser]
I use both, actually. I tend to prefer parameters for readability, as jedd points out, but sometimes it is very uncertain as to how many URI segments there may be - in which case I of course use URI segments directly.

One trick I used in one place was to use only the last uri segment as what I really wanted to know, and everything else is just navigational assistance and SEO keywords.

So there is no real reason you need to go and change everything you've done, but the big advantage of controller function parameters is just the clarity if what you are expecting. You know right there that "Hey, this function wants a userid, but this function called reset_password wants a userid and email, but email is optional, so that must mean..."

Etc. It can be easier to bug hunt and for you to look back at old code when you are explicit as to what a function expects, what is optional, and what you think is required for a function to work.
#6

[eluser]AzizLight[/eluser]
When I think of it, I totally agree with the readability point. The reason I used segments until now is because that's how I learned to do it in the first place.

Also, I actually haven't thought about the "variable number of segments" alternative, but that's a valid point, however I never encountered such a scenario (yet). But I think I'm going to use the controllers' methods parameter way of doing things until I have a good reason to change back.

Once again, thanks a lot for the answers Smile
#7

[eluser]vecima[/eluser]
I use method parameters, probably because I was a programmer before I was a web programmer. I see a function as something that should be able to be used as is, from wherever. if you use parameters, then you can call the function from anywhere else on your site, and not have to worry about whether that function is going to look at the right part of the URI.




Theme © iAndrew 2016 - Forum software by © MyBB