erdum / php-weekend
A simple minimal set of functions provide you the ability to build API & web application with no setup time
Requires
- php: >=5.1
This package is auto-updated.
Last update: 2024-09-08 08:40:48 UTC
README
A simple minimal set of functions provide you the ability to build API & web application with no setup time
Table of Contents
Features
- 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
Installation
Install PHP-Weekend with composer
composer require erdum/php-weekend
Or install without composer on older PHP versions just copy the src directory and require the App.php and Router.php
<?php require(__DIR__ . '/src/App.php'); require(__DIR__ . '/src/Router.php'); use PhpWeekend\Router; use PhpWeekend\App; Router::get('/', function() { App::send_json(array('data' => 'Hello, World!')); }); Router::get('/simple', function() { App::send_response('This is the content.', 200, 'text/html'); }); Router::get('/get_pdf', function() { App::send_file(__DIR__ . '/static/my_file.pdf'); });
Usage
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 } });
Feedback
If you have any feedback, please reach out to us at erdumadnan@gmail.com