PHP code example of math280h / php-router

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

    

math280h / php-router example snippets


use Math280h\PhpRouter\Router;
use Math280h\PhpRouter\Request;

session_start();

$router = new Router("./public");
$router->get('/', function () {
    echo 'Hello World';
});

$router->post('/', function (Request $request) {
    echo 'Hello World';
});

$router->run();

$router->get('/', MyController::class . '::index')
$router->post('/', MyController::class . '::index')

use Math280h\PhpRouter\Route;

$router->group(["prefix" => "test"], [
    Route::get('/1', function () {
        echo 'Hello World';
    }),
    Route::get('/2', function () {
        echo 'Hello World';
    }),
]);

$router->addMiddleware("log", function ($request) {
    print_r($request);
});

$router->get('/', function () {
    echo 'Hello World';
}, ['log']);

class MyController {
    public function index($request) {
        echo 'Hello World';
    }
}

$router->get('/', MyController::class . '::index');

class MyController {
    public function index($request) {
        $view_factory = new \Aura\View\ViewFactory;
        $view = $view_factory->newInstance();
        $view_registry = $view->getViewRegistry();
        $layout_registry = $view->getLayoutRegistry();
        $layout_registry->set('default', dirname(__DIR__) . '/views/layouts/default.php');
        $view_registry->set('page', dirname(__DIR__) . '/views/my-view.php');
        $view->setView('page');
        $view->setLayout($layout);
        return $view;
    }
}

$router->get('/', MyController::class . '::index');

/**
 * Returns a view Object
 *
 * @param string $path
 * @param array $data
 * @param string $layout
 * @return View
 */
function view(string $path, array $data = [], string $layout = 'default'): View
{
    $view_factory = new \Aura\View\ViewFactory;
    $view = $view_factory->newInstance();
    $view_registry = $view->getViewRegistry();
    $layout_registry = $view->getLayoutRegistry();
    $layout_registry->set('default', dirname(__DIR__) . '/views/layouts/default.php');
    $view_registry->set('page', dirname(__DIR__) . '/views/' . $path . '.php');
    $view->setView('page');
    $view->setLayout($layout);
    $view->addData($data);
    return $view;
}

composer