PHP code example of sn01615 / bramus-router

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

    

sn01615 / bramus-router example snippets


// Require composer autoloader
uter = new \Bramus\Router\Router();

// Define routes
// ...

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

$router->match('GET|POST', 'pattern', function() { … });

$router->get('pattern', function() { /* ... */ });
$router->post('pattern', function() { /* ... */ });
$router->put('pattern', function() { /* ... */ });
$router->delete('pattern', function() { /* ... */ });
$router->options('pattern', function() { /* ... */ });
$router->patch('pattern', function() { /* ... */ });

$router->all('pattern', function() { … });

// This route handling function will only be executed when visiting http(s)://www.example.org/about
$router->get('/about', function($name) {
    echo 'About Page Contents';
});

// Bad
$router->get('/hello/\w+', function($name) {
    echo 'Hello ' . htmlentities($name);
});

// Good
$router->get('/hello/(\w+)', function($name) {
    echo 'Hello ' . htmlentities($name);
});

$router->get('/movies/(\d+)/photos/(\d+)', function($movieId, $photoId) {
    echo 'Movie #' . $movieId . ', photo #' . $photoId);
});

$router->get('/movies/{movieId}/photos/{photoId}', function($movieId, $photoId) {
    echo 'Movie #' . $movieId . ', photo #' . $photoId);
});

$router->get('/movies/{foo}/photos/{bar}', function($movieId, $photoId) {
    echo 'Movie #' . $movieId . ', photo #' . $photoId);
});

$router->get(
    '/blog(/\d+)?(/\d+)?(/\d+)?(/[a-z0-9_-]+)?',
    function($year = null, $month = null, $day = null, $slug = null) {
        if (!$year) { echo 'Blog overview'; return; }
        if (!$month) { echo 'Blog year overview'; return; }
        if (!$day) { echo 'Blog month overview'; return; }
        if (!$slug) { echo 'Blog day overview'; return; }
        echo 'Blogpost ' . htmlentities($slug) . ' detail';
    }
);

$router->get('/blog(/\d+(/\d+(/\d+(/[a-z0-9_-]+)?)?)?)?', function($year = null, $month = null, $day = null, $slug = null) {
    // ...
});

$router->get('/blog(/\d{4}(/\d{2}(/\d{2}(/[a-z0-9_-]+)?)?)?)?', function($year = null, $month = null, $day = null, $slug = null) {
    // ...
});

$router->mount('/movies', function() use ($router) {

    // will result in '/movies/'
    $router->get('/', function() {
        echo 'movies overview';
    });

    // will result in '/movies/id'
    $router->get('/(\d+)', function($id) {
        echo 'movie id ' . htmlentities($id);
    });

});

    $router->ns('Admin')->prefix('/admin')->group(function (\Bramus\Router\Router $router) {
        $router->get('login', 'LoginController@login');
    });
    
    $router->prefix('/novel')->get('/nothing', function () {
        echo 'yes';
    });
    
    $router->ns('Admin')->prefix('/admin')->get('/login', 'LoginController@login1');
    $router->get('/login', 'LoginController@login2');
    
    $router->ns('Admin')->prefix('/admin')->group(function (\Bramus\Router\Router $router) {
        $router->get('login', 'LoginController@login3');
        $router->ns('Novel')->prefix('/novel')->group(function (\Bramus\Router\Router $router) {
            $router->get('info', 'NovelController@getNovelInfo');
        });
    });

$router->get('/(\d+)', '\App\Controllers\User@showProfile');

$router->setNamespace('\App\Controllers');
$router->get('/users/(\d+)', 'User@showProfile');
$router->get('/cars/(\d+)', 'Car@showProfile');

$router->get('/(\d+)', '\App\Controllers\User::showProfile');

$router->domain('admin.router.com')->group(function (\Bramus\Router\Router $router) {
    $router->get('login', 'LoginController@login');
});

$router->set404(function() {
    header('HTTP/1.1 404 Not Found');
    // ... do something special here
});

$router->set404('\App\Controllers\Error@notFound');

$router->before('GET|POST', '/admin/.*', function() {
    if (!isset($_SESSION['user'])) {
        header('location: /auth/login');
        exit();
    }
});

$router->before('GET', '/.*', function() {
    // ... this will always be executed
});

$router->run(function() { … });

$router->get('/', function() { echo 'Index'; });
$router->get('/hello', function() { echo 'Hello!'; });

// Override auto base path detection
$router->setBasePath('/');

$router->get('/', function() { echo 'Index'; });
$router->get('/hello', function() { echo 'Hello!'; });

$router->run();

$tpl = new \Acme\Template\Template();

$router->get('/', function() use ($tpl) {
    $tpl->load('home.tpl');
    $tpl->setdata(array(
        'name' => 'Bramus!'
    ));
});

$router->run(function() use ($tpl) {
    $tpl->display();
});

$router->put('/movies/(\d+)', function($id) {

    // Fake $_PUT
    $_PUT  = array();
    parse_str(file_get_contents('php://input'), $_PUT);

    // ...

});