|
| 1 | + |
| 2 | +# PHP-Weekend |
| 3 | + |
| 4 | +A simple minimal set of functions provide you the ability to build API & web application with no setup time |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- Routing with dynamic routes |
| 11 | +- Templating |
| 12 | +- Method for accessing request data |
| 13 | +- Method for sending json data |
| 14 | +- Customizable |
| 15 | +- No design restriction build your API in anyway you want |
| 16 | +- Zero configuration |
| 17 | + |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +Install PHP-Weekend |
| 22 | + |
| 23 | +```bash |
| 24 | + composer require erdum/php-weekend |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +After installing the package your project directory will have the following |
| 30 | + |
| 31 | +```bash |
| 32 | +ls |
| 33 | +``` |
| 34 | +- composer.json |
| 35 | +- composer.lock |
| 36 | +- vendor |
| 37 | + |
| 38 | +Create an index.php file |
| 39 | + |
| 40 | +```php |
| 41 | +<?php |
| 42 | + |
| 43 | +require(__DIR__ . '/vendor/autoload.php'); |
| 44 | + |
| 45 | +use PhpWeekend\Router; |
| 46 | +use PhpWeekend\App; |
| 47 | + |
| 48 | +Router::get('/', function() { |
| 49 | + App::send_json(['data' => 'Hello, World!']); |
| 50 | +}); |
| 51 | + |
| 52 | +/* |
| 53 | +Router also have |
| 54 | +post |
| 55 | +put |
| 56 | +patch |
| 57 | +delete |
| 58 | +any |
| 59 | +*/ |
| 60 | +``` |
| 61 | + |
| 62 | +You can get request data submitted in any format with a single function |
| 63 | +- query Parameters |
| 64 | +- form-data |
| 65 | +- multipart/form-data |
| 66 | +- application/json |
| 67 | + |
| 68 | +```php |
| 69 | +<?php |
| 70 | + |
| 71 | +require(__DIR__ . '/vendor/autoload.php'); |
| 72 | + |
| 73 | +use PhpWeekend\Router; |
| 74 | +use PhpWeekend\App; |
| 75 | + |
| 76 | +Router::post('/', function() { |
| 77 | + $request_payload = App::get_request(); |
| 78 | + |
| 79 | + App::send_json(['data' => $request_payload], 201); |
| 80 | +}); |
| 81 | +``` |
| 82 | + |
| 83 | +You can also build dynamic routes |
| 84 | +```php |
| 85 | +<?php |
| 86 | + |
| 87 | +require(__DIR__ . '/vendor/autoload.php'); |
| 88 | + |
| 89 | +use PhpWeekend\Router; |
| 90 | +use PhpWeekend\App; |
| 91 | + |
| 92 | +Router::get('/user/$name', function($name) { |
| 93 | + App::send_json(['data' => $name]); |
| 94 | +}); |
| 95 | +``` |
| 96 | + |
| 97 | +You can also use templates create a directory called templates in your project root |
| 98 | + |
| 99 | +```bash |
| 100 | +mkdir templates |
| 101 | +``` |
| 102 | +inside your templates directory you can build your templates like home.php |
| 103 | + |
| 104 | +```php |
| 105 | +<h1><?= $data ?></h1> |
| 106 | +<h2><?= $foo ?></h2> |
| 107 | + |
| 108 | +<?php if ($age > 18): ?> |
| 109 | + Hello |
| 110 | +<?php else: ?> |
| 111 | + whatsapp |
| 112 | +<?php endif; ?> |
| 113 | +``` |
| 114 | + |
| 115 | +now you can render home.php template from your index file |
| 116 | + |
| 117 | +```php |
| 118 | +<?php |
| 119 | + |
| 120 | +require(__DIR__ . '/vendor/autoload.php'); |
| 121 | + |
| 122 | +use PhpWeekend\Router; |
| 123 | +use PhpWeekend\App; |
| 124 | + |
| 125 | +Router::get('/', function() { |
| 126 | + App::send_template('home', [ |
| 127 | + 'data' => 'Hello, World!', |
| 128 | + 'foo' => 'bar', |
| 129 | + 'age' => 18 |
| 130 | + ]); |
| 131 | +}); |
| 132 | +``` |
| 133 | + |
| 134 | +App also provides you the csrf functionality |
| 135 | + |
| 136 | +```php |
| 137 | +<form> |
| 138 | + <input type="name" name="user-name"> |
| 139 | + <input type="email" name="user-email"> |
| 140 | + <?= set_csrf() ?> |
| 141 | +</form> |
| 142 | +``` |
| 143 | + |
| 144 | +now verify the csrf token in the handler |
| 145 | +```php |
| 146 | +<?php |
| 147 | + |
| 148 | +require(__DIR__ . '/vendor/autoload.php'); |
| 149 | + |
| 150 | +use PhpWeekend\Router; |
| 151 | +use PhpWeekend\App; |
| 152 | + |
| 153 | +Router::get('/', function() { |
| 154 | + |
| 155 | + if (App::is_csrf_valid()) { |
| 156 | + // CSRF token validated |
| 157 | + } |
| 158 | +}); |
| 159 | +``` |
| 160 | +## Feedback |
| 161 | + |
| 162 | +If you have any feedback, please reach out to us at erdumadnan@gmail.com |
| 163 | + |
| 164 | +[](https://choosealicense.com/licenses/mit/) |
| 165 | + |
0 commit comments