corrigindo rotas

This commit is contained in:
Reginaldo
2023-06-13 18:08:42 -03:00
parent 20fc580fe2
commit 00d21cc8c5
11 changed files with 182 additions and 22 deletions

View File

@@ -0,0 +1,17 @@
<?php
namespace app\controllers;
use League\Plates\Engine;
class Controller{
public static function view(string $view, array $data = []){
$viewsPath = dirname(__FILE__,2)."/views";
if(!file_exists($viewsPath . DIRECTORY_SEPARATOR . $view.".php")){
throw new \Exception("A view {$view} não existe");
}
$templates = new Engine($viewsPath);
echo $templates-> render($view,$data);
}
}

View File

@@ -2,5 +2,7 @@
namespace app\controllers;
class HomeController{
public function index(){
return Controller::view("home");
}
}

View File

@@ -2,5 +2,11 @@
namespace app\controllers;
class LoginController{
public function index(){
return Controller::view("login");
}
public function store(){
var_dump('store do login');
}
}

3
app/views/home.php Normal file
View File

@@ -0,0 +1,3 @@
<?php $this->layout("master");?>
<h1>Home</h1>

3
app/views/login.php Normal file
View File

@@ -0,0 +1,3 @@
<?php $this->layout("master");?>
<h1>Login</h1>

11
app/views/master.php Normal file
View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?=$this->section('content')?>
</body>
</html>

View File

@@ -11,5 +11,7 @@
"email": "resilvajr@hotmail.com"
}
],
"require": {}
"require": {
"league/plates": "^3.5"
}
}

83
composer.lock generated Normal file
View File

@@ -0,0 +1,83 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "0ae27c55eae141acc77e7a2bc0bb042f",
"packages": [
{
"name": "league/plates",
"version": "v3.5.0",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/plates.git",
"reference": "a6a3238e46c6e19af7318fdc36bfbe49b0620231"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/plates/zipball/a6a3238e46c6e19af7318fdc36bfbe49b0620231",
"reference": "a6a3238e46c6e19af7318fdc36bfbe49b0620231",
"shasum": ""
},
"require": {
"php": "^7.0|^8.0"
},
"require-dev": {
"mikey179/vfsstream": "^1.6",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"autoload": {
"psr-4": {
"League\\Plates\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jonathan Reinink",
"email": "jonathan@reinink.ca",
"role": "Developer"
},
{
"name": "RJ Garcia",
"email": "ragboyjr@icloud.com",
"role": "Developer"
}
],
"description": "Plates, the native PHP template system that's fast, easy to use and easy to extend.",
"homepage": "https://platesphp.com",
"keywords": [
"league",
"package",
"templates",
"templating",
"views"
],
"support": {
"issues": "https://github.com/thephpleague/plates/issues",
"source": "https://github.com/thephpleague/plates/tree/v3.5.0"
},
"time": "2023-01-16T20:25:45+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.3.0"
}

View File

@@ -1,19 +0,0 @@
<?php
function load(string $controller,string $action){
//se o controller existe
$controllerNamespace = "app\\controllers\\{$controller}";
var_dump($controller);
}
$rotas = [
"GET" =>[
"/"=> load("HomeController","index"),
"/login" => load("LoginController","index")
],
"POST"=>[
"contato" => load("ContatoController","store")
]
];

View File

@@ -1,6 +1,28 @@
<?php
require "vendor/autoload.php";
require "config/rotas.php";
require "routes/router.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");
}
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();
}catch(Exception $e){
$e->getMessage();
}
//header("Access-Control_Allow_Origin: *");
//header("Access-Control-Allow-Credentials: true");

30
routes/router.php Normal file
View File

@@ -0,0 +1,30 @@
<?php
function load(string $controller, string $action){
try{
$controllerNameSpace = "app\\controllers\\{$controller}";
if(!class_exists($controllerNameSpace)){
throw new Exception("Controller {$controller} não existe");
}
$controllerInstance = new $controllerNameSpace();
if(!method_exists($controllerInstance,$action)){
throw new Exception("O método {$action} não existe");
}
$controllerInstance->$action();
}catch(Exception $e){
echo $e->getMessage();
}
}
$router= [
"GET"=>[
"/projeto/api_educa/"=> fn()=> load("HomeController","index"),
"/projeto/api_educa/login" => fn()=> load("LoginController","index")
],
"POST"=>[
"/projeto/api_educa/login" => fn()=> load("LoginController","store")
]
];