PHP code example of ikkez / f3-middleware

1. Go to this page and download the library: Download ikkez/f3-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ikkez / f3-middleware example snippets


$f3 = magine 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();

// 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','*');
});

$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
}