A simple minimal set of functions provide you the ability to build API & web application with no setup time
- Routing with dynamic routes
- Templating
- Method for accessing request data
- Method for sending json data
- Customizable
- No design restriction build your API in anyway you want
- Zero configuration
Install PHP-Weekend
composer require erdum/php-weekend
After installing the package your project directory will have the following
ls
- composer.json
- composer.lock
- vendor
Create an index.php file
<?php
require(__DIR__ . '/vendor/autoload.php');
use PhpWeekend\Router;
use PhpWeekend\App;
Router::get('/', function() {
App::send_json(['data' => 'Hello, World!']);
});
/*
Router also have
post
put
patch
delete
any
*/
You can get request data submitted in any format with a single function
- query Parameters
- form-data
- multipart/form-data
- application/json
<?php
require(__DIR__ . '/vendor/autoload.php');
use PhpWeekend\Router;
use PhpWeekend\App;
Router::post('/', function() {
$request_payload = App::get_request();
App::send_json(['data' => $request_payload], 201);
});
You can also build dynamic routes
<?php
require(__DIR__ . '/vendor/autoload.php');
use PhpWeekend\Router;
use PhpWeekend\App;
Router::get('/user/$name', function($name) {
App::send_json(['data' => $name]);
});
You can also use templates create a directory called templates in your project root
mkdir templates
inside your templates directory you can build your templates like home.php
<h1><?= $data ?></h1>
<h2><?= $foo ?></h2>
<?php if ($age > 18): ?>
Hello
<?php else: ?>
whatsapp
<?php endif; ?>
now you can render home.php template from your index file
<?php
require(__DIR__ . '/vendor/autoload.php');
use PhpWeekend\Router;
use PhpWeekend\App;
Router::get('/', function() {
App::send_template('home', [
'data' => 'Hello, World!',
'foo' => 'bar',
'age' => 18
]);
});
App also provides you the csrf functionality
<form>
<input type="name" name="user-name">
<input type="email" name="user-email">
<?= set_csrf() ?>
</form>
now verify the csrf token in the handler
<?php
require(__DIR__ . '/vendor/autoload.php');
use PhpWeekend\Router;
use PhpWeekend\App;
Router::get('/', function() {
if (App::is_csrf_valid()) {
// CSRF token validated
}
});
If you have any feedback, please reach out to us at erdumadnan@gmail.com