rotas nomeadas corrigidas

This commit is contained in:
Reginaldo
2023-06-14 11:29:53 -03:00
parent 00d21cc8c5
commit 38cbb4dc8c
10 changed files with 64 additions and 20 deletions

View File

@@ -6,7 +6,7 @@
//criada a função para faz o get da tabela login
//conforme o id que está sendo passado para o select do banco
public function getLogin($NO_USERNAME){
if($NO_USERNAME>''){
if(!$NO_USERNAME==''){
$this->sql = "SELECT * FROM LOGIN WHERE NO_USERNAME = ?";
$this->stmt = self::$inst->prepare($this->sql);
$this->stmt->bindValue(1,$NO_USERNAME);

View File

@@ -1,8 +1,8 @@
<?php
namespace app\controllers;
use League\Plates\Engine;
class Controller{
use PDO;
class Controller extends DBA{
public static function view(string $view, array $data = []){
$viewsPath = dirname(__FILE__,2)."/views";
@@ -13,5 +13,37 @@ class Controller{
$templates = new Engine($viewsPath);
echo $templates-> render($view,$data);
}
}
public function getLogin($NO_USERNAME){
if(!$NO_USERNAME==''){
$this->sql = "SELECT * FROM LOGIN WHERE NO_USERNAME = ?";
$this->stmt = self::$inst->prepare($this->sql);
$this->stmt->bindValue(1,$NO_USERNAME);
$this->stmt->execute();
$this->res = $this->stmt->fetch(PDO::FETCH_OBJ);
return $this-> res;
}else{
//caso o id não foi passado recebe essa mensagem
die('informação incompleta...');
}
}
}
class DBA{
protected $stmt, $sql, $res;
protected static $inst;
//faz a chamada da instancia da conexão com o banco de dados
function __construct(){
try{
if(self::$inst === null){
self::$inst = new \PDO(STR_BD,USUARIO,SENHA);
}
//retorna a instancia com sucesso
//echo "Connected successfully";
return self::$inst;
}catch(\PDOException $e){
//retorna caso de erro na conexão
echo "Connection failed: " . $e->getMessage();
}
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace app\controllers;
require_once('app/classes/login.class.php');
class HomeController{
public function index(){
return Controller::view("home");

View File

@@ -2,10 +2,13 @@
namespace app\controllers;
class LoginController{
public function index(){
return Controller::view("login");
public function index($params){
$usuario = $params->NO_USERNAME;
$Log = new Controller();
$Logins = $Log->getLogin($usuario);
echo json_encode($Logins);
return json_encode($Logins);
}
public function store(){
var_dump('store do login');
}

View File

@@ -1,3 +1,3 @@
<?php $this->layout("master");?>
<h1>Home</h1>
rota não definida

View File

@@ -1,11 +1,13 @@
<?php
require "vendor/autoload.php";
require "routes/router.php";
require_once('app/classes/conexao.class.php');
try{
$uri = parse_url($_SERVER["REQUEST_URI"])['path'];
$request = $_SERVER["REQUEST_METHOD"];
if(!isset($router[$request])){
throw new Exception("A rota não existe");
}
@@ -13,10 +15,6 @@ try{
if(!array_key_exists($uri,$router[$request])){
throw new Exception("A rota não existe");
}
var_dump($uri);
var_dump($request);
$controller = $router[$request][$uri];
$controller();
@@ -54,7 +52,13 @@ try{
//url para retonar o login 61
//http://localhost/projeto/api_educa/index.php/?idf=61
//http://localhost/projeto/api_educa/index.php/?idf=66
//http://192.168.18.2/projeto/api_educa/index.php/?NO_USERNAME=AMENEZES
//http://192.168.18.2/?NO_USERNAME=AMENEZES
//aprendendo rotas
//https://www.youtube.com/watch?v=h7K-KUZ3Rvw
//https://www.youtube.com/watch?v=h7K-KUZ3Rvw
//dados na home
//http://localhost:8000/?name=reginaldo
//dados do login
//http://localhost:8000/login?NO_USERNAME=AMENEZES

View File

@@ -12,8 +12,7 @@ function load(string $controller, string $action){
if(!method_exists($controllerInstance,$action)){
throw new Exception("O método {$action} não existe");
}
$controllerInstance->$action();
$controllerInstance->$action((object)$_REQUEST);
}catch(Exception $e){
echo $e->getMessage();
}
@@ -21,10 +20,16 @@ function load(string $controller, string $action){
$router= [
"GET"=>[
"/projeto/api_educa/"=> fn()=> load("HomeController","index"),
"/projeto/api_educa/login" => fn()=> load("LoginController","index")
"/"=> fn()=> load("HomeController","index"),
"/login" => fn()=> load("LoginController","index")
],
"POST"=>[
"/projeto/api_educa/login" => fn()=> load("LoginController","store")
"/login" => fn()=> load("LoginController","store")
]
];
//rodar local host
//php -S localhost:8000
//depois chamar localhost:8000
//http://localhost:8000/login?NO_USERNAME=AMENEZES