PHP code example of joydeep-bhowmik / php-router

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

    

joydeep-bhowmik / php-router example snippets



use JoydeepBhowmik\PHPRouter\Router;
$router = new Router();
$router->get('/', function () {
    return 'Home';
});
$router->dispatch();
echo Router::$view;


$router->get('/', function () {
    return 'get request';
});
$router->post('/', function () {
    return 'post request';
});
$router->put('/', function () {
    return 'put request';
});
$router->delete('/', function () {
    return 'delete request';
});
$router->any('/', function () {
    return 'anytype of request';
});
$router->dispatch();

$router->get('*', function () {
    return '404 not found';
});

$router->get('/profile/{user}/{id}', function ($user, $id) {
    return 'Username  ' . $user . ' and user id is ' . $id;
});

$router->baseUrl('/shop');

$router->get('/profile/{user}/{id}', [ExampleController::class, 'index']);

$router->addRoute('profile/{user}/{id}', function () {
    return 'Hello';
}, 'METHOD_NAME');

class Example_middleware
{
    public function handle()
    {
        return true;
    }
}

$router->middleware(Example_middleware::class, function () use ($router) {

    $router->get('/', function () {
        return 'Home';
    });

});
$router->dispatch()