Fast is a fast, simple, extensible framework for PHP. Fast enables you to quickly and easily build RESTful web applications.
Adding repository to composer.json
"repositories": [
{
"type": "vcs",
"url": "https://github.com/0limits91/fast-framework.git"
}
],
Updating requires in composer.json
"require": {
"0limits91/fast-framework": "dev-main",
}
Now installing Fast-Framework with Composer
composer install
include __DIR__ . '/../src/autoload.php';
$app = new \FastFramework\Application();
$app->get('/', function ($req, $res) {
$res->send('Hello, World!');
});
$app->run();
//GET
$app->get('/', function ($req, $res) {
$res->send('Hello World!');
});
//POST
$app->post('/', function ($req, $res) {
$res->send('Got a POST request');
});
//PUT
$app->put('user', function ($req, $res) {
$res->send('Got a PUT request at /user');
});
//DELETE
$app->delete('user', function ($req, $res) {
$res->send('Got a DELETE request at /user');
});
//Defines a GET route for the path "route/:username/:id"
$app->get('route/:username/:id', function ($req, $res) {
$res->send(print_r($req->params, true));
});
//Parameter access
$username = $req->params['username'];
$id = $req->params['id'];
//Apply to all routes
$app->use('*',function($req, $res) {
$res->header('X-Service-Key', 'Example123456');
});
//Apply to specific route (home)
$app->use('/route',function($req, $res) {
$res->header('X-Service-Key', 'Example7891011');
});
//Define shortcode
$app->shortcode('hello', function() {
return 'world';
});
//Using shortcode
$app->get('/', function ($req, $res) {
$content = 'PHP says Hello [hello]!';
$res->send($content);
});
$app->get('/database',function(Request $req, Response $res) {
$database = $req->app->db;
$data = Array ("name" => "Francesco",
"createdAt" => $database->now(),
"updatedAt" => $database->now(),
);
$id = $database->insert ('tableName', $data);
$cols = Array("name");
$data = $database->get("tableName",null, $cols);
echo "last inserted id: $id";
$res->send($data);
});
$app->get('transient', function(Request $req, Response $res){
$cache = $req->app->cache;
//Get transient
$data = $cache->get('my_transient_key');
if ($data !== false) {
$res->send("Data from cache: " . $data);
} else {
echo "Cache expired or not found.";
}
$cache->set('my_transient_key', 'Fast Framework cached data', 3600); //valid for 1 our
$resData = $cache->get('my_transient_key');
// Remove transient
$cache->delete('my_transient_key');
$res->send($resData);
});