Rotte personalizzate in PHP

Autore: Fabio Di Matteo
Ultima revisione: 23/08/2023 - 16:40

Trovata questa classe che permette di specificare delle rotte personalizzate in php (Bramus Router) . Mostro brevemente l'utilizzo.

<?php
	# https://github.com/bramus/router/tree/master
	
	 require_once __DIR__ . '/Router.php';
 
	 // Create a Router
    $router = new \Bramus\Router\Router();
 
 
    /*
     *  $router->get('pattern', function() { });
		$router->post('pattern', function() {  });
		$router->put('pattern', function() {  });
		$router->delete('pattern', function() {  });
		$router->options('pattern', function() {  });
		$router->patch('pattern', function() { });
		$router->all('pattern', function() { });
     * */
 
	 $router->get('/', function () {
		 echo '<h1>Home page</h1>';
 
	 });
 
	 $router->get('/fabio', function () {
		 echo '<h1>Hello Fabio!</h1>';
		 echo 's='.$_GET['s'];
 
	 });
 
	  $router->set404(function () {
         echo '<h1>Pagina non trovata </h1>';
    });
 
 
 
	  $router->run();
 
?>

.htacces per apache

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]