PHP code example of stonks / router

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");
    }
}

// Rota
$router->get("/params/{category}/page/{page}", "Name:params", "name.params");

$this->router->route("name.params", [
    "category" => 22,
    "page" => 2
]);

// Resultado
https://www.{}/name/params/22/page/2

$this->router->route("name.params", [
    "category" => 22,
    "page" => 2,
    "argument1" => "most filter",
    "argument2" => "most search"
]);

// Resultado
https://www.{}/name/params/22/page/2?argument1=most+filter&argument2=most+search

$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';
}

/**
 * GET httpMethod
 */
$router->get("/", function ($data) {
    $data = ["realHttp" => $_SERVER["REQUEST_METHOD"]] + $data;
    echo "<h1>GET :: Spoofing</h1>", "<pre>", print_r($data, true), "</pre>";
});

/**
 * POST httpMethod
 */
$router->post("/", function ($data) {
    $data = ["realHttp" => $_SERVER["REQUEST_METHOD"]] + $data;
    echo "<h1>POST :: Spoofing</h1>", "<pre>", print_r($data, true), "</pre>";
});

/**
 * PUT spoofing e httpMethod
 */
$router->put("/", function ($data) {
    $data = ["realHttp" => $_SERVER["REQUEST_METHOD"]] + $data;
    echo "<h1>PUT :: Spoofing</h1>", "<pre>", print_r($data, true), "</pre>";
});

/**
 * PATCH spoofing e httpMethod
 */
$router->patch("/", function ($data) {
    $data = ["realHttp" => $_SERVER["REQUEST_METHOD"]] + $data;
    echo "<h1>PATCH :: Spoofing</h1>", "<pre>", print_r($data, true), "</pre>";
});

/**
 * DELETE spoofing e httpMethod
 */
$router->delete("/", function ($data) {
    $data = ["realHttp" => $_SERVER["REQUEST_METHOD"]] + $data;
    echo "<h1>DELETE :: Spoofing</h1>", "<pre>", print_r($data, true), "</pre>";
});

$router->dispatch();



$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://localhost/stonks/router/exemple/spoofing/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "PUT",
  CURLOPT_POSTFIELDS => "first_name=Giovanni&last_name=Oliveira&[email protected]",
  CURLOPT_HTTPHEADER => array(
    "Cache-Control: no-cache",
    "Content-Type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "Error cURL #:" . $err;
} else {
  echo $response;
}