Ensuring POST/GET etc variables are set?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Ensuring POST/GET etc variables are set?

Post by Dave2000 »

Below is a section of one of my bad controllers. What can I use so I don't have to keep writing something similar to the below every time I wish to call a model which uses user inputted (is that a word?) data... How should I ensure all the necessary POST/GET etc variables are set? It's this sort of thing, and some others, which often result in my controllers becoming way too longgggg.

Code: Select all

 
$this->newsModel = new newsModel($registry);
$POSTs = array('title', 'userName', 'body');
$inputs = array();
foreach ($POSTs as $key => $var )
{
    $inputs[$var] = empty($_POST[$var]) ? '' : $_POST[$var];
}
 
if ( isset($_POST['addNews']) )
{
    if ( $this->newsModel->add($inputs) == true )
    {
        redirect('news/');
    }
}
 
Thank you in advance if anyone is able to shed some light on this.

S
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Ensuring POST/GET etc variables are set?

Post by crazycoders »

What can help limit the validation code bloat that all programmers have to write is to have some kind of class or form definition code that helps you create and validate the forms. There will always be a bit of code to write, but having something like that can greatly help you reduce the number of lines and blocks of code used to validate.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Ensuring POST/GET etc variables are set?

Post by josh »

Shears wrote:Below is a section of one of my bad controllers.
ive written worse
Shears wrote: What can I use so I don't have to keep writing something similar to the below every time I wish to call a model which uses user inputted
If controller behavior is being duplicated it should be refactored into a controller layer super class. If it is verbose it should be "pushed down" into the model layer
Shears wrote: inputted (is that a word?) data...
"user input data", 'input' in this context can be used past tense I think
Shears wrote:How should I ensure all the necessary POST/GET etc variables are set?
These are request environment specific concepts, the controller should set up the path of communication for all request data to be seeded into your models, then the model should be asked if the data supplied was sufficient or not.
Post Reply