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
// 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();
}