PHP code example of kenphp / router
1. Go to this page and download the library: Download kenphp/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/ */
kenphp / router example snippets
$router->route('GET', '/users', ['UserController', 'listUsers']);
$router->get('/users/{id}', ['UserController', 'getUser']);
$router->head('/users', ['UserController', 'listUsers']);
$router->post('/users', ['UserController', 'createUser']);
$router->put('/users/{id}', ['UserController', 'updateUser']);
$router->delete('/users/{id}', ['UserController', 'deleteUser']);
$router->group('/api', function() use ($router) {
$router->get('/products/{id}', ['ProductController', 'getProduct']);
});
$router->setNotFoundHandler(function() {
echo 'Page not found.';
});
$router->get('/users/{id}', ['UserController', 'getUser'], [
'namespace' => 'app\controllers'
]);
$routeArray = $router->resolve('/users/1', 'GET');
/**
* $routeArray would contains
* [
* 'handler' => ['UserController', 'getUser'],
* 'params' => ['id' => 1],
* 'namespace' => 'app/controllers',
* ]
*/