Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ikkez/f3-middleware
a middleware router for PHP Fat-Free-Framework
https://github.com/ikkez/f3-middleware
fat-free-framework middleware router
Last synced: about 1 month ago
JSON representation
a middleware router for PHP Fat-Free-Framework
- Host: GitHub
- URL: https://github.com/ikkez/f3-middleware
- Owner: ikkez
- License: gpl-3.0
- Created: 2017-01-24T13:48:08.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-03-11T09:10:00.000Z (almost 6 years ago)
- Last Synced: 2024-11-02T01:37:33.430Z (2 months ago)
- Topics: fat-free-framework, middleware, router
- Language: PHP
- Size: 16.6 KB
- Stars: 23
- Watchers: 5
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![Middleware](https://ikkez.de/linked/f3-middleware.png)
## Middleware Router
This is a middleware router for PHP Fat-Free-Framework, version 3.x
It's based on the F3 core router, that can be called independently before or after the main routing cycle.
This can be useful if you want to hook into a group of other routes and want to do something right before processing the main route handler.### Installation
- Method 1: use composer composer require ikkez/f3-middleware
- Method 2: copy the `middleware.php` file into your F3 `lib/` directory or another directory that is known to the [AUTOLOADER](https://fatfreeframework.com/quick-reference#AUTOLOAD)
### Usage Samples
```php
$f3 = require('lib/base.php');// imagine you have some admin routes
$f3->route('GET|POST /admin','Controller\Admin->login');
// and these actions should be protected
$f3->route('GET|POST /admin/@action','Controller\Admin->@action');
$f3->route('GET|POST /admin/@action/@type','Controller\Admin->@action');
$f3->route('PUT /admin/upload','Controller\Files->upload');// so just add a global pre-route to all at once
\Middleware::instance()->before('GET|POST /admin/*', function(\Base $f3, $params, $alias) {
// do auth checks
});\Middleware::instance()->run();
$f3->run();
```Of course you could also use the `beforeroute` and `afterroute` events in your controller to add that auth check functionality. But in case your controller structure isn't ready yet for easy implementation or you have things you strictly want to separate from your controllers, like settings. Here the Middleware Router will aid you.
```php
// enable the CORS settings only for your API routes:
\Middleware::instance()->before('GET|HEAD|POST|PUT|OPTIONS /api/*', function(\Base $f3) {
$f3->set('CORS.origin','*');
});
```You can also create additional middleware wrappers on other events:
```php
$mw = \Middleware::instance();
$mw->on('limit',['GET @v1: /api/v1/*','GET @v2: /api/v2/*'], function($f3,$args,$alias) {
// do api usage limit checks
return false;
});if ($mw->run('limit')) {
// all good, continue
}else{
// API limit reached
}
```## Notice
Although you can probably use the middleware router as a completely stand alone router, it is not a full replacement of the frameworks integrated router.
The middleware router does not include handling of route caching, header management or automated CORS support, so use it as an additional component for settings, checks and workload before/after the main routing cycle.## License
You are allowed to use this plugin under the terms of the GNU General Public License version 3 or later.
Copyright (C) 2019 Christian Knuth (ikkez)