PHP code example of willry / micro-router

1. Go to this page and download the library: Download willry/micro-router 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/ */

    

willry / micro-router example snippets




use WillRy\MicroRouter\AppSingleton;
use WillRy\MicroRouter\Controller\UserController;
use WillRy\MicroRouter\Middleware\TestMiddleware;

thodNotAllowed');

$app->get('/', UserController::class, 'index')->name('home');

$app->get('/create-url', UserController::class, 'createUrl')->name('createUrl');


$app->middleware([
    new TestMiddleware()
], function ($app) {
    $app->get('/show/{id}', UserController::class, 'show')->name('show.user');
});

$app->post('/create', UserController::class, 'create')->name('create');

$app->get('/redirect', UserController::class, 'redirect')->name('redirect');

/**
 * Customize exception type handler
 *
 * Use for customize each exception type, like:
 * - \Exception
 * - AuthenticationException::class
 *
 *
 */
$app->handler(\Exception::class, function (\Exception $e) {
    http_response_code(500);
    var_dump('ooops');

    throw $e;
});

$app->run();




use WillRy\MicroRouter\AppSingleton;
use \WillRy\MicroRouter\Router\ActiveRoute;

class UserController
{
    public function index()
    {
        echo 'index';
    }

    public function show(array $data)
    {
        var_dump([
            'currentRoute' => ActiveRoute::getRoute(),
            'currentRouteParams' => ActiveRoute::getParams(),
            'controllerParams' => $data
        ]);
    }

    public function create()
    {
        $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        var_dump($post);
    }

    public function notFound()
    {
        echo 'Pagina não encontrada';
    }

    public function methodNotAllowed()
    {
        echo 'Rota com método não permitido';
    }
}



use \WillRy\MicroRouter\Middleware\MiddlewareInterface;

class TestMiddleware implements MiddlewareInterface
{

    // método que executa o middleware
    public function handle(array $data = [])
    {
        $rand = rand(0, 10) % 2 === 0;
        if (!$rand) {
            echo 'Random Error';
            die;
        }

    }
}



/**
 * Para customizar os tipos de exceptions
 *
 * Basta registrar o tipo da exception e o callback que executa para trata-la
 * - \Exception
 * - AuthenticationException::class
 *
 *
 */
 
 
//nesse exemplo eu customizo o status code e saída das exceptions comuns
//relançando elas com o novo status code
$app->handler(\Exception::class, function (\Exception $e) {
    http_response_code(500);
    var_dump('ooops');

    //relança a exception (opcional)
    throw $e;
});

//Nesse exemplo eu customizei o status code e saída
$app->handler(AuthenticationException::class, function (\Exception $e) {
    http_response_code(401);
    var_dump('Auth Failed');
});
shell
#iniciar servidor do php
php -S localhost:9090 -t public/