method params, validating urls in codeIgniter |
[eluser]1cookie[/eluser]
hi For example, a URL like: http://example.com/index.php/move/sku/qty and let's say the last two parameters go missing for whatever reason. Ive been coding around this issue as: Code: public function move($sku = FALSE, $qty=FALSE){ I'm initializing method arguments in advance to supress undefined variable warnings. While this is not nessecarily a bad thing - it's not ideal in ALL situations. I thought I'd ask the CI purists for a more elegant design. thanks in advance.
[eluser]Aken[/eluser]
You should check to make sure the parameter is in whatever format it should be in, instead of using the exclamation point method. For instance, SKU could exist, but it could be full of letters, when you're only expecting numbers. It would be valid to the code as you have it set now.
[eluser]1cookie[/eluser]
[quote author="Aken" date="1328909372"]You should check to make sure the parameter is in whatever format it should be in, instead of using the exclamation point method. [/quote] So if $sku is in the form 'E48' (which is normal in my system), would a regexp be a good idea maybe? i.e. Code: function move($sku, $qty){ I'm still using exclamation points though... ![]() I guess the point I'm trying to make is. Is there no built-in way of handling parameters like this. Or, moreover, is this just my lack of experience with MVC and CI in particular?
[eluser]Aken[/eluser]
Something like that, yeah. Those regex's could use a little work, but on the right track. ![]()
[eluser]1cookie[/eluser]
[quote author="Aken" date="1328914449"]Something like that, yeah. Those regex's could use a little work, but on the right track. ![]() admittedly, the regexp's need some work. Few beers, lol.. thanks.. ![]()
[eluser]1cookie[/eluser]
[quote author="Aken" date="1328909372"]You should check to make sure the parameter is in whatever format it should be in, [/quote] I missed my point I was trying to make. Consider this method: Code: public function method($param) If i call this like so: http://example.com/index.php/controller/method/param with a missing $param variable, this will result in a PHP warning: A PHP Error was encountered Severity: Warning Message: Missing argument 1 for Controller::method() Filename: controllers/controller.php Line Number: 18 I can get around this with method signitures like so: Code: public function method($param=null){} is this normal in CI or is there any better ways of handling missing url query strings?
[eluser]1cookie[/eluser]
[quote author="Aken" date="1328989799"]Yes that's normal.[/quote] arh, thanks Aken.... |
Welcome Guest, Not a member yet? Register Sign In |