PHP code example of lighthouse / router

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

    

lighthouse / router example snippets


use Lighthouse\Router\Router;

$router = new Router();

// Register routes
$router->get('/users', 'UsersController@index');
$router->post('/users', 'UsersController@store');
$router->get('/users/{id}', 'UsersController@show');
$router->put('/users/{id}', 'UsersController@update');
$router->delete('/users/{id}', 'UsersController@destroy');

$router->get('/users/{id}', function ($id) {
    return "User: {$id}";
});

$router->get('/posts/{postId}/comments/{commentId}', function ($postId, $commentId) {
    return "Post {$postId}, Comment {$commentId}";
});

// Match the route
$match = $router->matchRoute('GET', '/users/123');
$match->getParameter('id'); // "123"
$match->getParameters();    // ['id' => '123']

$router->group('/api', function (Router $router) {
    $router->get('/users', 'ApiUsersController@index');
    $router->get('/posts', 'ApiPostsController@index');
});

// Creates:
// GET /api/users
// GET /api/posts

$router->group('/api', function (Router $router) {
    $router->group('/v1', function (Router $router) {
        $router->get('/users', 'handler');
    });
    
    $router->group('/v2', function (Router $router) {
        $router->get('/users', 'handler');
    });
});

// Creates:
// GET /api/v1/users
// GET /api/v2/users

$router->get('/users', 'handler')->name('users.index');
$router->get('/users/{id}', 'handler')->name('users.show');
$router->get('/users/{id}/posts/{postId}', 'handler')->name('users.posts.show');

// Generate URLs
$router->url('users.index');                           // /users
$router->url('users.show', ['id' => 123]);             // /users/123
$router->url('users.posts.show', ['id' => 1, 'postId' => 42]); // /users/1/posts/42

// Match specific methods
$router->match(['GET', 'POST'], '/form', 'FormController@handle');

// Match all methods
$router->any('/api', 'ApiController@handle');

use Lighthouse\Router\Exception\RouteNotFoundException;
use Lighthouse\Router\Exception\MethodNotAllowedException;

try {
    $match = $router->dispatch($request);
    
    $handler = $match->getHandler();
    $params = $match->getParameters();
    
    // Call your handler with parameters
} catch (RouteNotFoundException $e) {
    // 404 - Route not found
} catch (MethodNotAllowedException $e) {
    // 405 - Method not allowed
    $allowedMethods = $e->getAllowedMethods();
}

$match = $router->matchRoute('GET', '/users/123');

catch (RouteNotFoundException $e) {
    $e->getMethod(); // "GET"
    $e->getPath();   // "/unknown"
}

catch (MethodNotAllowedException $e) {
    $e->getMethod();         // "DELETE"
    $e->getAllowedMethods(); // ["GET", "POST"]
}