1. Go to this page and download the library: Download szenis/routing 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/ */
szenis / routing example snippets
zenis\Routing\Router;
$router = new Router();
$router->get('/{n:date}-{w:item}', function($date, $item) {
return 'hello world';
});
$response = $router->resolve($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
switch ($response['code']) {
case \Szenis\Routing\Route::STATUS_NOT_FOUND:
// render your 404 page here...
break;
case \Szenis\Routing\Route::STATUS_FOUND:
// the router only resolves the route, here is an example how to execute the route.
if ($response['handler'] instanceof \Closure) {
echo call_user_func_array($response['handler'], $response['arguments']);
}
break;
}
/**
* initialize the router class
*/
$router = new Router();
/**
* Route matches the url '/' for the GET method
*/
$router->add('/', 'GET', function() {
// the closure will be executed when route is triggerd and will return 'hello world'
return 'hello world!';
});
/**
* It is posible to add one or multiple wildcards in one route
*/
$router->add('/user/{id}', 'GET', function($id) {
return $id;
});
/**
* It is also posible to allow mulitple methods for one route (methods should be separated with a '|')
*/
$router->add('/user/{id}/edit', 'GET|POST', function($id) {
return 'user id: '.$id;
});
/**
* Or when u are using controllers in a namespace you could give the full path to a controller (controller::action)
*
* Since version 2.0 executing the handler is up to you.
*/
$router->add('/user/{id}/delete', 'DELETE', 'App\Controllers\UserController::delete');
/**
* Since version 1.1 there are shortcut methods for get, post, put, patch, delete and any.
* You can use them as follow
*/
$router->get('/example/get', function() {}); // Will match GET requests
$router->post('/example/post', function() {}); // Will match POST requests
$router->put('/example/put', function() {}); // Will match PUT requests
$router->patch('/example/patch', function() {}); // Will match PATCH requests
$router->delete('/example/delete', function() {}); // Will match DELETE requests
$router->any('/example/any', function() {}); // Will match GET, POST, PUT, PATCH, DELETE requests
/**
* resolve the route and receive the response
*
* The response is an array with the following keys
* - code (contains 200 if the route is found, else 404)
* - handler (contains the handler, often a \Closure or full path to your controller action)
* - arguments (contains the route arguments if any)
*/
$response = $router->resolve($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
/**
* To execute your route you have to do the following
*/
switch ($response['code']) {
case \Szenis\Routing\Route::STATUS_NOT_FOUND:
// render your 404 page here...
break;
case \Szenis\Routing\Route::STATUS_FOUND:
// the router only resolves the route, here is an example how to execute the route.
if ($response['handler'] instanceof \Closure) {
echo call_user_func_array($response['handler'], $response['arguments']);
}
// if your handler is a full path to your function you could execute it with this:
$className = substr($response['handler'], 0, strpos($response['handler'], '::'));
$functionName = substr($response['handler'], strpos($response['handler'], '::') + 2);
echo call_user_func_array(array((new $className), $functionName), $response['arguments']);
break;
}
// The id must be a number
$router->add('/user/{n:id}', 'GET', 'App\Controllers\UserController::show');
// The id must contain either alfabetic chars or numbers
$router->add('/user/{an:id}', 'GET', 'App\Controllers\UserController::show');
// Now we want everything behind docs/ in the page variable
// For example when we go to the url /docs/user/edit we will receive user/edit in the page variable
$router->add('/docs/{*:page}', 'GET', function($page) {
// do something with $page
});
// Optional parameter example
$router->add('/hello/{a:name}/{?:lastname}', 'GET', function($name, $lastname = null) {
// check if lastname is provided
// if ($lastname) {...}
})
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.