Should I access the POST parameters in the model or pass as a method argument from the controller?

I need to process about 20 POST parameters, and I'm not sure where to do it.

I could define each as an argument to a method on the model and pass them from the controller when the method was called. This will lead to pretty little work and make the function call less clear due to the number of arguments.

Or I could call the method on the model and just access the parameters.

Passing parameters as arguments will give me more control over the parameters accessed by the function, and the documentation will be more understandable. But if new parameters were added later, they will need to be added to the end of the method call so as not to interrupt every existing call. I assume this will become rather confusing if this happens several times, since the arguments cannot be logically grouped.

If I refer to a parameter in the model, the parameters should not be passed from the controller to the model, which makes the call method a term. But I do not control the parameters that are accessed, since they can easily be added or deleted without restrictions. This will require more discipline from other developers, and I don’t like to depend on it, because sooner or later someone must "just (add | change | fix) this real speed."

I'm not sure where to go. I usually do all this in a model, since it is faster to write, it seems to be easier to maintain (without chaos of arguments) and is conceptually better suited for my understanding of the model. On the other hand, I’m not sure that my idea of ​​the model is correct, and if it ends in chaos, if I depend on other developers, to always update the documentation after each change.

So what should I do?

+5
source share
5 answers

Well, why can't you just take a (associative) array as a parameter in this model method and then pass the entire $ _POST array to it? At least in my opinion, this will not break encapsulation.

EDIT: , " " (, , , ). , :

class UserData
{
    protected $name;
    protected $username;
    protected $password;

    public function getName() { /* <...> */ }
    public function setName() { /* <...> */ }
    /* other accessors go here */
}

class UserController extends Controller
{
    public function register()
    {
        $userData = UserData::create()
            ->setName($_POST['name'])
            ->setUsername($_POST['username'])
            ->setPassword($_POST['password']);
        Users::add($userData);
    }
}

Users:: add, .

+1

, , , , : . , , :

$user = new User();
$user->setName($_POST['lastName'],$_POST['firstName']);
$user->setAddress($_POST['address1'],$_POST['address2'],$_POST['city'],$_POST['state'],$_POST['zip']);

. .

, , . .

+1

. , . ( ). , .

0

. - . , , thats bad

0

...

//Object Oriented POST Replacement
  if ($_SERVER['REQUEST_METHOD'] == 'POST')
  {
      $_POST = json_decode(file_get_contents('php://input'));
  }

API, JSON Content-Type: application/json. (, , ) , $_POST .

, $_POST . -.

From there, simply set the controller methods to invoke model methods with super-global object-oriented $_POSTas the only argument.

0
source

All Articles