PHP code example of marrios / router

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

    

marrios / router example snippets


composer 

    
use Marrios\Router\HttpRouter;

$router = new HttpRouter();

// Set route
$router->get("/helloworld", [function(){ echo "Hello World!";}])->run();
$router->notFound();

 

    Hello World!
 

use App\Controllers\TesteController;

use Marrios\Router\HttpRouter;

$router = new HttpRouter();

// Set route
$router->post("/helloworld", [TesteController::class, "helloWorld"])->run();
$router->notFound();

 

    Hello World!
 

use Marrios\Router\HttpRouter;

$router = new HttpRouter();

// Set route
$router->post("/blog/{category}/{id_post}", [ function($param){ echo $param->category;}])->run();
$router->notFound();

 

    video
 

use Marrios\Router\HttpRouter;

$router = new HttpRouter();

// Instantiating the route object
$router = new Router();

// Set route
$router->get("/blog/{category}/{id_post}", [TesteController::class, "helloWorld"])->run();
$router->notFound();

 

class TesteController
{
    public function helloWorld($param)
    {
        echo $param->id_post;
    }
}

    1323
 


$router->group([
    $router->get("ok",  [function () {echo "Hello";}])->run(),
    $router->get("ok2",  [function () {echo "Hello 2";}])->run()
]);



$router->middleware([Middleware::class])->group([
    $router->get("ok",  [function () {echo "Hello";}])->run(),
    $router->get("ok2",  [function () {echo "Hello 2";}])->run()
]);

 

$router->logs(logs: true)->setStorageLogs(__DIR__);