Where to check user login?

I am developing a new application using an object-oriented approach involving some REST, I do not use any frameworks.

I have a question where it is best to check the user login to the setter, as shown below:

public function setSalary($salary)
{
    if (Validator::money($salary))
        $this->salary = $salary;
    else
        return 'Error that is an invalid number';
}

Or in the controller?

public function updateSalary()
{
    $errors = array();

    if (Validator::money($_POST['salary']))
        $salary = $_POST['salary'];
        else
            $errors ['salary']  = 'Error that is an invalid number';

    if(count($errors))
        return $errors;

    $employee = new Employee($_POST['e_Id']);
    $employee->setSalary($salary);

    $employee->save();
}

If I were to install the installer, what should my controller look like and return verification errors?

, , , , , , . , , .

?

+4
5

, , , MVC- , , .

  • , PHP, . , , . HTML, <input>.

  • for if , . , , PSR-1 PSR-2.

  • . , , .

. .

:

  • ( Employee) -. , .

    :

    $employee = new Entity\Employee;
    $employee->setID($id);
    $employee->setSalary($money);    
    if ($employee->isValid()) {
        $mapper = new Mapper\Employee($dbConn);
        $mapper->store($emplyee);
    }
    
  • DDD, - . , , (, ).

, , : . , . , UNIQUE.

as, , .

+7

, . . . - , , , , . , . , - :

public function updateSalary()
{
    $employee = new Employee($_POST['e_Id']);
    $employee->setSalary($_POST['salary']));
    if ($employee->validate()) {
        $employee->save();             
    } else {
        return $employee->getErrors();
    }
}

:

  • . , , validate(). ;
  • validate() - validate() . validate , - . () , Employee.
  • (, ), validate() , .
+1

, "", , / , , , .

0

-, , , .

, , , , , .

, - , -, , -, , .

0

, . , , , .

, , , . , , .

, (, NOT NULL ).

, () . , . PDO :

$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

, :

public function setSalary($salary) {
    if (!Validator::money($salary)) {
        throw new Exception('Invalid value provided as salary.');
    }
    $this->salary = $salary;
}

: $errors, , View. , , .

Employee , :

public function updateSalary($emp_id, $salary) {
    try {    
        // Note that any of the following statements could trigger exceptions:
        $employee = $this->$model->getEmployee($emp_id);
        $employee->setSalary($salary);
        $employee->save();
    } catch(Exception $e) {
        $this->$model->logError('salary', $e->getMessage());
    }
}

, , . PHP :

$model = new Model();
$controller = new Controller($model);
$view = new View($controller, $model);

$controller->updateSalary($_POST['e_Id'], $_POST['salary']);

echo $view->output();

, .

, , , , ( ),... .., . .

0
source

All Articles