Created
March 16, 2024 21:15
-
-
Save johnwmcarneiro/c97f64daf8b358f6c98f57efe0db959a to your computer and use it in GitHub Desktop.
MVC, Repository example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PDO; | |
# App\Pessoa.php | |
class Pessoa | |
{ | |
public $id; | |
public $name; | |
public $gender; | |
} | |
# App\Repositories\AbstractRepository.php | |
abstract class AbstractRepository | |
{ | |
private $pdo; | |
public function __construct(PDO $pdo) | |
{ | |
$this->pdo = $pdo; | |
} | |
} | |
# App\Repositories\PessoaRepository.php | |
namespace App\Repositories; | |
use App\Pessoa; | |
use App\Repositories\AbstractRepository; | |
class PessoaRepository extends AbstractRepository | |
{ | |
private $model = Pessoa::class; | |
public function all($id) | |
{ | |
$id = intval($id); | |
$sql = "select id, name, gender from pessoas"; | |
$sth = $pdo->prepare($sql); | |
$sth->execute(); | |
return $sth->fetchAll(PDO::FETCH_CLASS, $this->model); | |
} | |
public function find($id) | |
{ | |
$id = intval($id); | |
$sql = "select id, name, gender from pessoas where id = {$id}"; | |
$sth = $pdo->query($sql); | |
$sth->setFetchMode(PDO::FETCH_CLASS, $this->model); | |
return $sth->fetch(); | |
} | |
} | |
# App\Controllers\AbstractController | |
abstract class AbstractController | |
{ | |
public $pdo; | |
public function __construct(PDO $pdo) | |
{ | |
$this->pdo = $pdo; | |
$this->beforeAction(); | |
} | |
/** | |
* HOOK | |
*/ | |
protected function beforeAction() | |
{ | |
} | |
} | |
# App\Controllers\PessoasController | |
namespace App\Controllers\PessoasController; | |
use App\Repositories\AbstractController; | |
use App\Repositories\PessoaRepository; | |
class PessoasController extends AbstractController | |
{ | |
protected $pessoaRepository; | |
protected function beforeAction() | |
{ | |
$this->pessoaRepository = new PessoaRepository($this->pdo); | |
} | |
public function indexAction() | |
{ | |
$pessoas = $this->pessoaRepository->all(); | |
var_dump($pessoas); | |
} | |
public function showAction() | |
{ | |
// $id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT); | |
// VERIFICAR ESSa função filter_input | |
$id = intval($_GET['id']); | |
$pessoa = $this->pessoaRepository->find($id); | |
var_dump($pessoa); | |
} | |
} | |
# App/Router.php | |
namespace App; | |
class Router | |
{ | |
private $routes; | |
public function __construct(array $routes) | |
{ | |
$this->routes = $routes; | |
} | |
public function hasRoute($pagina) | |
{ | |
return array_key_exists($pagina, $this->routes); | |
} | |
public function run($pagina) | |
{ | |
if (isset($_GET['acao']) && $_GET['acao'] != '') { | |
$acao = filter_input(INPUT_GET, 'acao', FILTER_SANITIZE_ENCODED); | |
} else { | |
$acao = 'index'; | |
} | |
$acao .= 'Action'; | |
$nameController = $this->routes[$pagina]; | |
if (class_exists($nameController)) { | |
$controller = new $nameController; | |
if (method_exists($controller, $acao)) { | |
$controller->$acao(); | |
} else { | |
$this->pageNotFound(); | |
} | |
} else { | |
$this->pageNotFound(); | |
} | |
} | |
public function pageNotFound() | |
{ | |
echo "Erro 404: Page not found!"; | |
} | |
} | |
# public/index.php | |
use App\Router; | |
use App\Controllers\PessoasController; | |
$routes = [ | |
'pessoas' => PessoasController::class | |
]; | |
$request = $_SERVER['REQUEST_URI']; | |
$parsed_uri = parse_url($request); | |
$router = new Router($'routes'); | |
$router->run($parsed_uri['path']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment