PHP does not recognize connection as parameter

1

I'm doing a system using MVC (the first time I use it in PHP), and when I go to login it appears:

  Uncaught ArgumentCountError: Too few arguments to function Login :: login (), 2 passed in C: \ wamp64 \ www \ php_pdo \ controller \ logincontroller.php on line 19 and exactly 3 expected in C: \ wamp64 \ www \ php_pdo \ model \ login.php

     

Too few arguments to function Login :: login (), 2 passed in C: \ wamp64 \ www \ php_pdo \ model C: \ wamp64 \ www \ php_pdo \ model \ login.php on line 29

In case the error says that a parameter is missing, I have tested the post of the login, and the 2 values are going (password and user), and conexãO is going too, but it still has these errors. .

Codes:

View: sign-in.php:

    <form method="post" action="" >
        <input type="text" name="usuario" placeholder="Digite seu nome de usuário"><br>
        <input type="password" name="senha" placeholder="Digite sua senha"><br>
        <input type="submit" name="btn" value="Entrar"><br>
    </form>   

    <?php
    // caso haja post, esse if será executado
    if($_POST){
        // Aqui ele vai filtar o post, o ultimo parametro serve para tirar tags e caracteres especiais.
        $usuario = filter_input(INPUT_POST, 'usuario', FILTER_SANITIZE_STRING);
        $senha = filter_input(INPUT_POST, 'senha', FILTER_SANITIZE_STRING);
        // aqui ele inclui a classe
        require_once '../controller/logincontroller.php';
        // aqui ele chama o método Login, por ser static não precisa criar um objeto.
        LoginController::logar($usuario, $senha);

    } 

Controller logincontroller.php:

require_once '../model/login.php';

class LoginController {

 static function logar($usuario, $senha){
    $result = Login::login($usuario, $senha);
    if ($result){
        echo '<script> confirm("Bem vindo ao sistema!");
                       window.location("menu"); <script>';
            // header("Location: menu");
    } else {
        echo '<script> confirm("Senha ou usuário incorreto!");
                       window.location("sign-in"); <script>';
    }
 }

}

Model login.php:

require_once '../projeto/connectionfactory.php';

class Login {

private $usuario;
private $senha;
private $con;

function __construct($usuario, $senha) {
    $this->usuario = $usuario;
    $this->senha = $senha;
    $this->con = ConnectionFactory::getConnection();
}

static function login($usuario, $senha, $con) {
    // isso é para tratar erros
    try {
        // aqui cria a transação, tudo que for feito dentro, fica protegido
        $con->beginTransaction();
        // puxar senha
        $stmt = $con->prepare("select senha from tbl_login where usuario = :usuario");
        $stmt->bindParam('usuario', $usuario);
        $stmt->execute();
        $hash = null;
        while ($obj = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $hash = $obj['senha'];
        }
        if (password_verify($senha, $hash)) {
            return true;
        } else {
            return false;
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}
}

Project connectionfactory.php:

class ConnectionFactory{
static function getConnection(){
    $db = 'tec_find';
    $user = 'root';
    $psw = '';
    try {
        $con = new PDO("mysql:host=localhost;dbname=$db", $user, $psw); 
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $con;
    } catch (Exception $ex) {
        echo $ex->getMessage();
    }   
}
static function testarConnection(){
    if(ConnectionFactory::getConnection()){
        // entra sempre aqui
        echo 'Conectado';
    } else { 
        echo 'Conectado';
    }

}
}

Folder structure:

    
asked by anonymous 14.07.2018 / 05:33

2 answers

3

Leandro Good evening.

You have created a class with a __construct and a static method, and in that __contruct you passed your connection, but have a problem, when we use the static method we do not instantiate the class, so __construct is wrong to exist there.

If you want to continue doing this, try this.

require_once '../projeto/connectionfactory.php';

class Login {

private $usuario;
private $senha;

public function __construct($usuario, $senha) {
    $this->usuario = $usuario;
    $this->senha = $senha;
}

public static function login($usuario, $senha) {
     $con = ConnectionFactory::getConnection();
    // isso é para tratar erros
    try {
        // aqui cria a transação, tudo que for feito dentro, fica protegido
        $con->beginTransaction();
        // puxar senha
        $stmt = $con->prepare("select senha from tbl_login where usuario = :usuario");
        $stmt->bindParam('usuario', $usuario);
        $stmt->execute();
        $hash = null;
        while ($obj = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $hash = $obj['senha'];
        }
        if (password_verify($senha, $hash)) {
            return true;
        } else {
            return false;
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}
}
    
16.07.2018 / 04:02
1

@Leandro is actually accusing that only 2 arguments have been passed and expected 3, you are calling the function Login::login like this:

$result = Login::login($usuario, $senha);

But it is defined like this:

static function login($usuario, $senha, $con) {

Try this:

$result = Login::login($usuario, $senha, $this->con);
    
14.07.2018 / 14:21