Skip to content

Vitor-Guedes/guedes-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Router

Pacote para criar rotas, para aplicações com arquitetura MVC.

Instanciação

É possivel mudar o separador que vai identificar a classe e funcão a ser executados nas rotas.

use GuedesRouter\RouteCollection;

$collection = new RouterCollection();

// ou
$collection = new RouterCollection(':');

Rota Basica

use GuedesRouter\RouteCollection;

$collection = new RouterCollection();

$collection->get('/', function () {
    return "Hello World!";
});

Verbos Http Disponiveis

/** trait GuedesRouter/Traits/Verbs */
$collection->get($uri, $callaback);
$collection->post($uri, $callaback);
$collection->put($uri, $callaback);
$collection->delete($uri, $callaback);

Rotas com parametros

$collection->get('/customer/{name}', function ($name) {
    return "Customer Id: $name";
});

$colection->post('/country/{country}/state/{state}', function ($country, $state) {
    return "Pais: $country, Estado: $state";
});

Callaback das Rotas

$collection = new RouterCollection(':');

/** closure */
$collection->get('/', function () {
    return "Hello World!";
});

/** string - Separador ':' definido na criação da $collection por default o separador é o '@' */
$collction->post('/store', 'Namespace/Controllers/IndexController:index');

/** array */
$collection->put('/customer/{id}', [CustomerController:class, 'update']);

Execução da rota

$route = $collection->get('/', function () {
    return "Hello World!";
})

$response = $route->resolve();

Identificação da rota

$collection->get('/', function () {
    return "Página html renderezida.";
})
$collection->get('/contact', function () {
    return "Página html renderezida.";
})
$collection->get('/about', function () {
    return "Página html renderezida.";
})

$requestUri = $_SERVER['REQUEST_URI'];
$requestMethod = $_SERVER['REQUEST_METHOD'];

$matchedRoute = null;
foreach ($this->routerCollection->getRoutes($requestMethod) as $route) {
    if ($route->match($requestUri)) {
        $matchedRoute = $route;
        break ;
    }
}

if ($matchedRoute) {
    echo $matchedRoute->resolve();
}