CodeIgniter Forums
method params, validating urls in codeIgniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: method params, validating urls in codeIgniter (/showthread.php?tid=49192)



method params, validating urls in codeIgniter - El Forum - 02-10-2012

[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){
            
            if(!$sku || !$qty){ // Problem with the URL params, (malformed URL)
          
                    $data['page_title'] = 'Error!';
                    $this->load->view('header', $data);
                    $this->load->view('error', $data);
                    $this->load->view('footer');
                    return;
            } else { // OK, return view
                    $data['page_title'] = 'Load the intended page';
                    $this->load->view('header', $data);
                    $this->load->view('index', $data);
                    $this->load->view('footer');

              }

           //...

}

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.


method params, validating urls in codeIgniter - El Forum - 02-10-2012

[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.


method params, validating urls in codeIgniter - El Forum - 02-10-2012

[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){
            
            if(!preg_match(/^[A-Z0-9]$/, $sku) || !preg_match(/[0-9]/, $qty) ){
          
                  //error
                    return;

            } else { // OK..
                     // load the views..

              }

}



I'm still using exclamation points though...Smile

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?









method params, validating urls in codeIgniter - El Forum - 02-10-2012

[eluser]Aken[/eluser]
Something like that, yeah. Those regex's could use a little work, but on the right track. Smile


method params, validating urls in codeIgniter - El Forum - 02-10-2012

[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. Smile[/quote]

admittedly, the regexp's need some work. Few beers, lol..

thanks..Smile


method params, validating urls in codeIgniter - El Forum - 02-11-2012

[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(isset($param))
{
            // ok...
} else {
  // problem with url, throw an exception

}

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?


method params, validating urls in codeIgniter - El Forum - 02-11-2012

[eluser]Aken[/eluser]
Yes that's normal.


method params, validating urls in codeIgniter - El Forum - 02-11-2012

[eluser]1cookie[/eluser]
[quote author="Aken" date="1328989799"]Yes that's normal.[/quote]


arh, thanks Aken....