rotas nomeadas corrigidas
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
//criada a função para faz o get da tabela login
|
//criada a função para faz o get da tabela login
|
||||||
//conforme o id que está sendo passado para o select do banco
|
//conforme o id que está sendo passado para o select do banco
|
||||||
public function getLogin($NO_USERNAME){
|
public function getLogin($NO_USERNAME){
|
||||||
if($NO_USERNAME>''){
|
if(!$NO_USERNAME==''){
|
||||||
$this->sql = "SELECT * FROM LOGIN WHERE NO_USERNAME = ?";
|
$this->sql = "SELECT * FROM LOGIN WHERE NO_USERNAME = ?";
|
||||||
$this->stmt = self::$inst->prepare($this->sql);
|
$this->stmt = self::$inst->prepare($this->sql);
|
||||||
$this->stmt->bindValue(1,$NO_USERNAME);
|
$this->stmt->bindValue(1,$NO_USERNAME);
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace app\controllers;
|
namespace app\controllers;
|
||||||
use League\Plates\Engine;
|
use League\Plates\Engine;
|
||||||
|
use PDO;
|
||||||
class Controller{
|
class Controller extends DBA{
|
||||||
public static function view(string $view, array $data = []){
|
public static function view(string $view, array $data = []){
|
||||||
|
|
||||||
$viewsPath = dirname(__FILE__,2)."/views";
|
$viewsPath = dirname(__FILE__,2)."/views";
|
||||||
@@ -13,5 +13,37 @@ class Controller{
|
|||||||
|
|
||||||
$templates = new Engine($viewsPath);
|
$templates = new Engine($viewsPath);
|
||||||
echo $templates-> render($view,$data);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace app\controllers;
|
namespace app\controllers;
|
||||||
|
require_once('app/classes/login.class.php');
|
||||||
class HomeController{
|
class HomeController{
|
||||||
public function index(){
|
public function index(){
|
||||||
return Controller::view("home");
|
return Controller::view("home");
|
||||||
|
|||||||
@@ -2,10 +2,13 @@
|
|||||||
namespace app\controllers;
|
namespace app\controllers;
|
||||||
|
|
||||||
class LoginController{
|
class LoginController{
|
||||||
public function index(){
|
public function index($params){
|
||||||
return Controller::view("login");
|
$usuario = $params->NO_USERNAME;
|
||||||
|
$Log = new Controller();
|
||||||
|
$Logins = $Log->getLogin($usuario);
|
||||||
|
echo json_encode($Logins);
|
||||||
|
return json_encode($Logins);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(){
|
public function store(){
|
||||||
var_dump('store do login');
|
var_dump('store do login');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
<?php $this->layout("master");?>
|
<?php $this->layout("master");?>
|
||||||
|
|
||||||
<h1>Home</h1>
|
rota não definida
|
||||||
|
|||||||
16
index.php
16
index.php
@@ -1,11 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
require "vendor/autoload.php";
|
require "vendor/autoload.php";
|
||||||
require "routes/router.php";
|
require "routes/router.php";
|
||||||
|
require_once('app/classes/conexao.class.php');
|
||||||
|
|
||||||
try{
|
try{
|
||||||
|
|
||||||
$uri = parse_url($_SERVER["REQUEST_URI"])['path'];
|
$uri = parse_url($_SERVER["REQUEST_URI"])['path'];
|
||||||
$request = $_SERVER["REQUEST_METHOD"];
|
$request = $_SERVER["REQUEST_METHOD"];
|
||||||
|
|
||||||
if(!isset($router[$request])){
|
if(!isset($router[$request])){
|
||||||
throw new Exception("A rota não existe");
|
throw new Exception("A rota não existe");
|
||||||
}
|
}
|
||||||
@@ -13,10 +15,6 @@ try{
|
|||||||
if(!array_key_exists($uri,$router[$request])){
|
if(!array_key_exists($uri,$router[$request])){
|
||||||
throw new Exception("A rota não existe");
|
throw new Exception("A rota não existe");
|
||||||
}
|
}
|
||||||
|
|
||||||
var_dump($uri);
|
|
||||||
var_dump($request);
|
|
||||||
|
|
||||||
$controller = $router[$request][$uri];
|
$controller = $router[$request][$uri];
|
||||||
$controller();
|
$controller();
|
||||||
|
|
||||||
@@ -54,7 +52,13 @@ try{
|
|||||||
//url para retonar o login 61
|
//url para retonar o login 61
|
||||||
//http://localhost/projeto/api_educa/index.php/?idf=61
|
//http://localhost/projeto/api_educa/index.php/?idf=61
|
||||||
//http://localhost/projeto/api_educa/index.php/?idf=66
|
//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
|
//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
|
||||||
@@ -12,8 +12,7 @@ function load(string $controller, string $action){
|
|||||||
if(!method_exists($controllerInstance,$action)){
|
if(!method_exists($controllerInstance,$action)){
|
||||||
throw new Exception("O método {$action} não existe");
|
throw new Exception("O método {$action} não existe");
|
||||||
}
|
}
|
||||||
|
$controllerInstance->$action((object)$_REQUEST);
|
||||||
$controllerInstance->$action();
|
|
||||||
}catch(Exception $e){
|
}catch(Exception $e){
|
||||||
echo $e->getMessage();
|
echo $e->getMessage();
|
||||||
}
|
}
|
||||||
@@ -21,10 +20,16 @@ function load(string $controller, string $action){
|
|||||||
|
|
||||||
$router= [
|
$router= [
|
||||||
"GET"=>[
|
"GET"=>[
|
||||||
"/projeto/api_educa/"=> fn()=> load("HomeController","index"),
|
"/"=> fn()=> load("HomeController","index"),
|
||||||
"/projeto/api_educa/login" => fn()=> load("LoginController","index")
|
"/login" => fn()=> load("LoginController","index")
|
||||||
],
|
],
|
||||||
"POST"=>[
|
"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
|
||||||
|
|||||||
Reference in New Issue
Block a user