1. Go to this page and download the library: Download easy-projects/simple-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/ */
easy-projects / simple-router example snippets
EasyProjects\SimpleRouter\Router as Router;
$router = new Router();
/*
Import only the packages and classes that you are using.
Improves application loading performance.
For this you need to gestione your project by packages.
*/
$router->autoload();
//Cors rules for the browser
$router->cors()->setAllowedOrigins("https://easyprojects.tech/", "localhost");
$router->cors()->setAllowedMethods("GET", "POST", "PUT", "DELETE");
$router->cors()->setAllowedHeaders("Content-Type", "Authorization");
//Routes
$router->get("/get/{idUser}",
//Middlewares for example
fn () => Middleware::auth(),
//Controller
fn () => Router::$response->status(200)->send(Router::$request->params->idUser)
);
$router->post("/add",
fn () => Router::$response->status(200)->send(Router::$request->body->nameUser)
);
$router->put("/update/{idUser}",
fn () => Router::$response->status(200)->send(Router::$request->params->idUser." - ".Router::$request->body->nameUser)
);
$router->delete("/delete/{idUser}",
fn () => Router::$response->status(200)->send(Router::$request->params->idUser." - ".Router::$request->body->nameUser)
);
//Use only in development
$router->start();