PHP code example of hexbit / router

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

    

hexbit / router example snippets


// for wordpress projects
use Hexbit\Router\WordPress\Router;

// first init router
add_action("init", function () {
     Router::init();
});

// Creates a route that matches the uri `/posts/list` both GET 
// and POST requests. 
Router::map(['GET', 'POST'], 'posts/list', function () {
    return 'Hello World';
});

// use this class for non wordpress systems
use Hexbit\Router\Router;

// Creates a route that matches the uri `/posts/list` both GET 
// and POST requests. 
Router::map(['GET', 'POST'], 'posts/list', function () {
    return 'Hello World';
});

Router::map(['GET'], 'posts/{id}', function(RouteParams $params, Request $request) {
    return $params->id;
});

Router::map(['GET'], 'posts/all', function () {})->name('posts.index');

$url = Router::url('posts.index');

Router::map(['GET'], 'posts/{id}', function () {})->name('posts.show');

$url = Router::url('posts.show', ['id' => 123]);

Router::get('test/route', function () {});
Router::post('test/route', function () {});
Router::put('test/route', function () {});
Router::patch('test/route', function () {});
Router::delete('test/route', function () {});
Router::options('test/route', function () {});

Router::setBasePath('base/path');
Router::map(['GET'], 'route/uri', function () {}); // `/base/path/route/uri`

// TestController.php
namespace \MyNamespace;

class TestController
{
    public function testMethod()
    {
        return 'Hello World';
    }
}

// routes.php
Router::map(['GET'], 'route/uri', '\MyNamespace\TestController@testMethod');

Router::group('api/v1/', function ($group) {
    $group->map(['GET'], 'route1', function () {}); // `/prefix/route1`
    $group->map(['GET'], 'route2', function () {}); // `/prefix/route2§`
});

// bool|Response
$response = Router::match();

// send response if route matches with current request
if ($response && $response->getStatusCode() !== Response::HTTP_NOT_FOUND) {
            $response->send();
            exit();
}

// by default laods page-custom-admin-login.php
$loginPage = new VirtualPage('custom-admin-login', 'Admin Login Title');

Router::virtualPage('login-admin/', $loginPage);