Last active
October 22, 2016 19:33
-
-
Save wilcorrea/30edd2f7522d7590e1c7c0680402f356 to your computer and use it in GitHub Desktop.
Router.php
This file contains 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 | |
define('__APP_ROOT__', __DIR__); | |
require_once 'vendor/autoload.php'; | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Meu Curso Legal</title> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.2.3/css/bulma.min.css" integrity="sha256-F7gqKszCwmz8vhiti+AICU8dLfIEpxzPVihhhGfbbKg=" crossorigin="anonymous" /> | |
</head> | |
<body> | |
<nav class="nav has-shadow"> | |
<div class="container"> | |
<div class="nav-left"> | |
<a class="nav-item is-brand" href="./"> | |
<img src="http://fagoc.br/download/a/logo" alt="logo"/> | |
</a> | |
</div> | |
<div class="nav-right nav-menu"> | |
<a class="nav-item" href="./6">Exercio 6</a> | |
<a class="nav-item" href="./7">Exercio 7</a> | |
<a class="nav-item" href="./8">Exercio 8</a> | |
</div> | |
</div> | |
</nav> | |
<pre> | |
<?php | |
use Fagoc\Core\Router; | |
$router = new Router(); | |
$router->get('/exercicio/:exercicio', function($exercicio) { | |
var_dump($outra); | |
$exercicio = __DIR__ . '/exercicio-' . $exercicio . '/' . 'file.php'; | |
if (file_exists($exercicio)) { | |
require_once $exercicio; | |
} | |
}); | |
$router->run(); | |
?> | |
</pre> | |
</body> | |
</html> |
This file contains 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 | |
namespace Fagoc\Core; | |
class Router | |
{ | |
private $method = 'GET'; | |
private $uri = ''; | |
/** | |
* routes[ | |
* 'GET' => [ | |
* '/' => 'faço isso', | |
* '/exercicio/6' => 'carregar o arquivo do exercicio 6' | |
* ] | |
* ] | |
*/ | |
private $routes = []; | |
public function __construct($uri = null, $method = null) | |
{ | |
if (!is_null($uri)) { | |
$this->uri = $uri; | |
} | |
if (!is_null($method)) { | |
$this->method = $method; | |
} | |
$this->parseRequest(); | |
} | |
private function parseRequest() | |
{ | |
$self = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : ''; | |
$peaces = explode('/', $self); | |
array_pop($peaces); | |
$start = implode('/', $peaces); | |
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; | |
$search = '/' . preg_quote($start, '/') . '/'; | |
$uri = preg_replace($search, '', $request_uri, 1); | |
$this->uri = $uri; | |
$this->method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : $this->method; | |
} | |
public function __call($name, $arguments) | |
{ | |
$method = strtoupper($name); | |
if (!isset($this->routes[$method])) { | |
$this->routes[$method] = []; | |
} | |
$peaces = explode('/', $arguments[0]); | |
foreach ($peaces as $key => $value) { | |
if (strpos($value, ':') === 0) { | |
$peaces[$key] = '(\w+)'; | |
} | |
} | |
$pattern = str_replace('/', '\/', implode('/', $peaces)); | |
$route = '/^' . $pattern . '$/'; | |
$callback = $arguments[1]; | |
$this->routes[$method][$route] = $callback; | |
} | |
public function run() | |
{ | |
$routes = $this->routes[$this->method]; | |
foreach($routes as $route => $callback) { | |
if (preg_match($route, $this->uri, $params)) { | |
//$route === $this->uri | |
array_shift($params); | |
return call_user_func_array($callback, array_values($params)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment