1
A little help hehe
I’m having trouble creating a login validation in a pattern MVC.
I think I managed to structure it right and my login screen appear in the index. So far everything okay.
When I submit the form that starts my headache, follow the codes:
When I submit the form, it returns the following error:
Warning: include_once(MODEL/Model.php): failed to open stream: No such file or directory in C:\xamppNew\htdocs\futamador\CONTROLLER\ControllerLogin.php on line 2
Warning: include_once(): Failed opening 'MODEL/Model.php' for inclusion (include_path='C:\xamppNew\php\PEAR') in C:\xamppNew\htdocs\futamador\CONTROLLER\ControllerLogin.php on line 2
Fatal error: Uncaught Error: Class 'Model' not found in C:\xamppNew\htdocs\futamador\CONTROLLER\ControllerLogin.php:14 Stack trace: #0 C:\xamppNew\htdocs\futamador\CONTROLLER\ControllerLogin.php(27): ControllerLogin->__construct() #1 {main} thrown in C:\xamppNew\htdocs\futamador\CONTROLLER\ControllerLogin.php on line 14
Index
if (isset($_POST[''])){
throw new Exception("Erro ao carregar <b>index.php</b>");
} else {
include_once("CONTROLLER/Controller.php");
$controller = new Controller();
$controller->login();
}
Controller
include_once("MODEL/Model.php");
class Controller {
public $model;
public $view;
private $usuario;
private $senha;
public function __construct(){
$this->model = new Model();
}
public function login(){
include_once("VIEW/login.php");
}
}
Controllerlogin
include_once("..\MODEL\Model.php");
class ControllerLogin {
private $model;
private $usuario;
private $senha;
function __construct(){
if( isset($_POST )){
$this->usuario = $_POST['usuario'] ? $_POST['usuario'] : null;
$this->senha = $_POST['senha'] ? $_POST['senha'] : null;
$this->model = new Model();
validaLogin( $this->usuario, $this->senha );
} else {
throw new Exception("Erro ao receber dados para login (<b>ControllerLogin.php</b>.");
}
}
private function validaLogin($usuario, $senha){
$this->model->exibirDados($usuario, $senha);
}
}
new ControllerLogin();
Model
include_once("MODEL/ConexaoContaModel.php");
class Model extends ConexaoConta {
function __construct(){
}
public function exibirDados($usuario, $senha){
echo $usuario . " - " . $senha;
}
}
Login form of the View
<form method="POST" action="CONTROLLER/ControllerLogin.php">
<h2>Login</h2>
<label for="inputEmail" class="sr-only">Usuário</label>
<input type="text" name="usuario" placeholder="Digitar o Usuário" required autofocus><br />
<label for="inputPassword" class="sr-only">Senha</label>
<input type="password" name="senha" placeholder="Digite a Senha" required >
<button class="btn btn-lg btn-primary btn-block" type="submit">Acessar</button>
</form>
Forgive me the immense post! But I would be grateful for a light :)
The archive
Model.php
is a directory above the directory CONTROLLER? So the right thing isinclude_once("../MODEL/Model.php");
– NoobSaibot
The folders are like this: ROOT > CONTROLLER | MODEL | VIEW | Index.php. In the folder CONTROLLER > Controller.php, Controllerlogin.php
– Cristiano Mendes
Read this question and the answer: Difference between './', '.. /' and '/'
– NoobSaibot
@Noobsaibot thank you very much! I had not noticed. I gave a
__DIR__
and saw that the path really did not exist. It still returns error, but is related to another part of the code– Cristiano Mendes
If you need help with this other error, you can create a new question. I opened voting to close this question.
– NoobSaibot
@Thank you Noobsaibot! The other mistake was because one was missing
this->
in my Controllerlogin.php constructor when you called the methodvalidaLogin()
– Cristiano Mendes
For those who have the same file route problem, using the full path helps a lot (
C:/pasta/pasta/pasta/arquivo.php
orhttps://meusite/pasta/arquivo.php
).– Cristiano Mendes