PHP code example of khantloonthu / php-router

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

    

khantloonthu / php-router example snippets


// Require composer autoloader
outer;

// Create Router instance
$router = new Router();

// Define routes
// ...

// Run it!
$router->run();

$router->get('/', function() {
    return "Hello World";
});

$router->get('/about-us', fn() => 
$router->post('/pattern', function() {
    // ...
});

$router->put('/pattern', function() {
    // ...
});

$router->delete('/pattern', function() {
    // ...
});


// Required parameter
$router->get('/user/{id}', function($id) {
    return "User ID: " . $id;
});

// Multiple , Comment ID: $commentId";
});


function checkLoggedIn($request) {
    global $isLoggedIn;

    if (!$isLoggedIn) {
        return ['status' => 401, 'message' => 'Unauthorized: Please log in.'];
    }

    return true; // continue processing
}

$router->middleware('checkLoggedIn');

// or anonymous function style

$router->middleware(function ($request) {
    global $isLoggedIn;

    if (!$isLoggedIn) {
        return false; // triggers 403 Forbidden by default
    }

    return true;
});


$router->get('/dashboard', function () {
    return  false;
        if (!$isAdmin) return false;
        return true;
    }
]);


$router->post('/create-user', function () use ($router) {
    $isAdmin = false;

    if (!$isAdmin) {
        $router->abort(403);
    }

    // continue with user creation...
});


$router->post('/send-mail', function () use ($router) {
    // send mail logic
    $mail->send();

    $router->redirect('/');
});


$router->handle(404, function() {
    return 

composer