isolucoesweb / router
Roteamento para MVC!
dev-master
2020-09-23 02:03 UTC
Requires
- php: ^7.2
This package is auto-updated.
Last update: 2025-02-23 11:49:31 UTC
README
<?php require __DIR__ . "/../vendor/autoload.php"; use ISWEB\Router\Router; $router = new Router("https://www.youdomain.com"); /** * routes * The controller must be in the namespace Test\Controller * this produces routes for route, route/$id, route/{$id}/profile, etc. */ $router->namespace("Test"); $router->get("/route", "Controller:method"); $router->post("/route/{id}", "Controller:method"); $router->put("/route/{id}/profile", "Controller:method"); $router->patch("/route/{id}/profile/{photo}", "Controller:method"); $router->delete("/route/{id}", "Controller:method"); /** * group by routes and namespace * this produces routes for /admin/route and /admin/route/$id * The controller must be in the namespace Dash\Controller */ $router->group("admin")->namespace("Dash"); $router->get("/route", "Controller:method"); $router->post("/route/{id}", "Controller:method"); /** * Group Error * This monitors all Router errors. Are they: 400 Bad Request, 404 Not Found, 405 Method Not Allowed and 501 Not Implemented */ $router->group("error")->namespace("Test"); $router->get("/{errcode}", "Error:notFound"); /** * This method executes the routes */ $router->dispatch(); /* * Redirect all errors */ if ($router->error()) { $router->redirect("/error/{$router->error()}"); }
Named
<?php require __DIR__ . "/../vendor/autoload.php"; use CoffeeCode\Router\Router; $router = new Router("https://www.youdomain.com"); /** * routes * The controller must be in the 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"); /** * This method executes the routes */ $router->dispatch(); /* * Redirect all errors */ if ($router->error()) { $router->redirect("name.hello"); }
Named Controller Exemple
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"); } }
Named Params
//route $router->get("/params/{category}/page/{page}", "Name:params", "name.params"); //$this->route = return URL //$this->redirect = redirect URL $this->router->route("name.params", [ "category" => 22, "page" => 2 ]); //result https://www.{}/name/params/22/page/2 $this->router->route("name.params", [ "category" => 22, "page" => 2, "argument1" => "most filter", "argument2" => "most search" ]); //result https://www.{}/name/params/22/page/2?argument1=most+filter&argument2=most+search
Callable
/** * 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 and httpMethod */ $router->put("/", function ($data) { $data = ["realHttp" => $_SERVER["REQUEST_METHOD"]] + $data; echo "<h1>PUT :: Spoofing</h1>", "<pre>", print_r($data, true), "</pre>"; }); /** * PATCH spoofing and httpMethod */ $router->patch("/", function ($data) { $data = ["realHttp" => $_SERVER["REQUEST_METHOD"]] + $data; echo "<h1>PATCH :: Spoofing</h1>", "<pre>", print_r($data, true), "</pre>"; }); /** * DELETE spoofing and httpMethod */ $router->delete("/", function ($data) { $data = ["realHttp" => $_SERVER["REQUEST_METHOD"]] + $data; echo "<h1>DELETE :: Spoofing</h1>", "<pre>", print_r($data, true), "</pre>"; }); $router->dispatch();
Form Spoofing
This example shows how to access the routes (PUT, PATCH, DELETE) from the application. You can see more details in the sample folder. From an attention to the _method field, it can be of the hidden type.
<form action="" method="POST"> <select name="_method"> <option value="POST">POST</option> <option value="PUT">PUT</option> <option value="PATCH">PATCH</option> <option value="DELETE">DELETE</option> </select> <input type="text" name="first_name" value="Aaaa"/> <input type="text" name="last_name" value="Bbbb"/> <input type="text" name="email" value="user@email.com"/> <button>Button</button> </form>
PHP cURL exemple
<?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "http://localhost/coffeecode/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=Paul&last_name=Joseph&email=user%40email.com", 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 "cURL Error #:" . $err; } else { echo $response; }