1. Go to this page and download the library: Download ginger-tek/routy 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/ */
ginger-tek / routy example snippets
use GingerTek\Routy;
$app = new Routy;
$app = new Routy;
// Standard Function
$app->get('/', function (Routy $app) {
$app->sendData('Hello!');
});
// Arrow Function
$app->get('/', fn () => $app->sendData('Hello!'););
// Closure
$handler = function (Routy $app) {
$app->sendData('Hello!');
};
$app->get('/closure', $handler);
// Static Class Method
class ProductsController {
public static function list(Routy $app) {
...
$app->sendJson($products);
}
}
$app->get('/products', \ProductsController::list(...));
$app = new Routy([
'root' => '../',
// i.e. app istance is in public/index.php and your app root is one directory above
'layout' => 'default',
// this will use the layout file at "layouts/default.php", respective of app root if set
'base' => '/api',
// i.e. your app files are in /wwwroot/my-api-app and is accessed via https://domain.com/api
])
new \PDO('sqlite:' . $app->getConfig('root') . '/app.db');
$app->get('/products', ...); // HTTP GET
$app->post('/products/:id', ...); // HTTP POST
$app->put('/products', ...); // HTTP PUT
$app->patch('/products/:id', ...); // HTTP PATCH
$app->delete('/products/:id', ...); // HTTP DELETE
$app->any('/products/:id', ...); // HTTP GET, POST, PUT, PATCH, DELETE, HEAD, or OPTIONS
$app->get('*', ...); // HTTP GET for all routes
$app->any('*', ...); // Any standard HTTP method for all routes
$app->route('GET|POST', '/form', ...); // HTTP GET and POST for the /form route
$app->route('GET|POST|PUT', '/products', ...); // HTTP GET, POST and PUT for the /products route
$app->setCtx('db', new PDO('sqlite:myData.db'));
...
$app->get('/products', function (Routy $app) {
$db = $app->getCtx('db');
$stmt = $db->prepare('select * from products');
$stmt->execute();
$result = $stmt->fetch();
...
})
$app = new Routy;
$app->group('/products', function (Routy $app) {
$app->post('/', ...);
$app->get('/', ...);
$app->get('/:id', ...);
$app->put('/:id', ...);
$app->delete('/:id', ...);
});
$app->group('/products', authenticate(...), function (Routy $app) {
$app->get('/', ...);
});
$app = new Routy;
$app->group('/products', function (Routy $app) {
$app->get('/', fn (Routy $app) => $app->sendJson([]));
// GET /products/asdf will end up here
$app->fallback(fn () => $app->render('product-not-found'));
});
// GET /asdf will end up here
$app->fallback(fn () => $app->render('not-found'));
$app = new Routy;
$app->group('/api', ApiController::index(...));
$app->serveStatic('/nm', 'node_modules');
$app->serveStatic('/', 'public');
$app->get('/', function (Routy $app) {
$app->uri; // /some/route
$app->method; // GET, POST, PUT, etc.
$app->params->someParam; // <= /route/with/:someParam
});
$name = $app->getQuery('name'); // <= /some/route?name=John%20Doe
echo $name; // John Doe
$app->post('/upload', function (Routy $app) {
$files = $app->getFiles('field-name');
// Destructure assignment for a single file upload
[$file] = $app->getFiles('field-name');
});