U
U
Uber Noob2018-11-28 14:57:56
PHP
Uber Noob, 2018-11-28 14:57:56

Interaction between controller and model?

Authorization function from controller:

// Авторизовать юзера с помощью электронной почты
    public function maillogin()
    {
        $POST = $_POST;
        $POST['mail'] = trim($POST['mail']);

        $validate = new Validator();

        if(!empty($_POST['mail']) && !empty($_POST['pwd']) && $validate->correctMail($_POST['mail']) === true){

      // В модель ничего не передаётся т.к. она возьмёт данные из POST
            $data = $this->userModel->{__FUNCTION__}();

            if($data === 'WrongPassword'){
                $view = new ErrorView();
                $view->wrongPwd();
            }
      elseif($data === 'WrongLogin') {
                $view = new ErrorView();
                $view->wrongLogin();
            }

        } else {
            $view = new ErrorView();
            $view->noData();
        }
    }

A fragment of the same function from the model:
if(password_verify($POST['pwd'], $pwd[0]['pwd'])){
        $_SESSION['ok'] = 'Вы успешно авторизованы';
        header("Location: {$_SERVER['HTTP_REFERER']}");
        die();
    }

Questions about this code:
1. The method maillogin()exists both in the controller and in the model, is it normal, or is it better to do it, mailloginController()and mailloginModel()
2. If the user logs in successfully, then the method in the model apparently does not return true, the session is created in the model and the user is sent to the page where it came from. This is bad? Should I return true and redirect in the controller?
3. By creating an instance of the class new ErrorView()in advance, for example, in the same place where new Validator()I can shorten the code below, and not write instantiation in each, ifbut if everything goes ok, then it will be needed and it will be a waste of memory. What is the best way to do it in this case?
4. die() I don’t like it in the model, but I don’t know how to replace it, as is usually done in such cases?
Well, if there are any other comments - I'm glad to hear.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-11-28
@ubernoob

1. Normal. OOP was created for this, that a method with the same name can be in different classes, but work in its own way, if this is consistent with the general logic.
2. For good, the model should simply describe the data, in extreme cases, return the data sets associated with it, but not return instances of third-party classes
3. Not the case where you need to worry about wasting memory. You do not create 10,000 copies. But it could be done nicely
so that the errorView constructor returns a view by the name of the error, and the functions wrongLogin and wrongPwd would be private.
4) Why is there die at all?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question