1. Go to this page and download the library: Download stonks/router library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stonks / router example snippets
Stonks\Router\Router;
$router = new Router("https://www.seudominio.com");
/**
* Rotas
* O controlador deve estar no namespace Test\Controller
* isto produz rotas para: rota, rota/$id, rota/{$id}/perfil, etc.
*/
$router->namespace("Test");
$router->get("/rota", "Controller:method");
$router->post("/rota/{id}", "Controller:method");
$router->put("/rota/{id}/perfil", "Controller:method");
$router->patch("/rota/{id}/perfil/{foto}", "Controller:method");
$router->delete("/rota/{id}", "Controller:method");
/**
* Agrupar por rotas e namespace
* O controlador deve estar no namespace Dash\Controller
* isto produz rotas para: /admin/rota e /admin/rota/$id
*/
$router->group("admin")->namespace("Dash");
$router->get("/rota", "Controller:method");
$router->post("/rota/{id}", "Controller:method");
/**
* Grupo de erro
* Isso monitora todos os erros do Router. São eles:
* 400 Pedido ruim (Bad Request)
* 404 Não encontrado (Not Found)
* 405 Método não permitido (Method Not Allowed)
* 501 Não implementado (Not Implemented)
*/
$router->group("error")->namespace("Test");
$router->get("/{errcode}", "Stonk:notFound");
/**
* Este método executa as rotas
*/
$router->dispatch();
/*
* Redireciona todos os erros
*/
if ($router->error()) {
$router->redirect("/error/{$router->error()}");
}
Stonks\Router\Router;
$router = new Router("https://www.seudominio.com");
/**
* Rotas
* O controlador deve estar no namespace Test\Controller
*/
$router->namespace("Test")->group("name");
$router->get("/", "Name:home", "name.home");
$router->get("/hello", "Name:hello", "name.hello");
$router->get("/redirect", "Name:redirect", "name.redirect");
/**
* Este método executa as rotas
*/
$router->dispatch();
/*
* Redireciona todos os erros
*/
if ($router->error()) {
$router->redirect("name.hello");
}
class Name
{
// ...
public function __construct($router)
{
$this->router = $router;
}
public function home(): void
{
echo "<h1>Home</h1>";
echo "<p>", $this->router->route("name.home"), "</p>";
echo "<p>", $this->router->route("name.hello"), "</p>";
echo "<p>", $this->router->route("name.redirect"), "</p>";
}
public function redirect(): void
{
$this->router->redirect("name.hello");
}
}
$router->get('/perfil', 'Controller:profile');
$router->get('/contato', 'Controller:contact'); // Exemplo: esta foi a rota executada
$router->currentRoute();
$router->get('/perfil', 'Controller:profile', 'controller.profile');
$router->get('/contato', 'Controller:contact', 'controller.contact'); // Exemplo: esta foi a rota executada
if($router->isCurrentRoute('controller.contact')){
echo 'A rota contato foi executada';
}