PHP code example of soanix / router
1. Go to this page and download the library: Download soanix/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/ */
soanix / router example snippets
// Require composer autoloader
/ Define routes
// ...
// Run it!
Router::run();
Router::match('GET|POST', 'pattern', function() { … });
Router::get('pattern', function() { /* ... */ });
Router::post('pattern', function() { /* ... */ });
Router::put('pattern', function() { /* ... */ });
Router::delete('pattern', function() { /* ... */ });
Router::options('pattern', function() { /* ... */ });
Router::patch('pattern', function() { /* ... */ });
Router::all('pattern', function() { … });
// This route handling function will only be executed when visiting http(s)://www.example.org/about
Router::get('/about', function() {
echo 'About Page Contents';
});
// Bad
Router::get('/hello/\w+', function($name) {
echo 'Hello ' . htmlentities($name);
});
// Good
Router::get('/hello/(\w+)', function($name) {
echo 'Hello ' . htmlentities($name);
});
Router::get('/movies/(\d+)/photos/(\d+)', function($movieId, $photoId) {
echo 'Movie #' . $movieId . ', photo #' . $photoId;
});
Router::get('/movies/{movieId}/photos/{photoId}', function($movieId, $photoId) {
echo 'Movie #' . $movieId . ', photo #' . $photoId;
});
Router::get('/movies/{foo}/photos/{bar}', function($movieId, $photoId) {
echo 'Movie #' . $movieId . ', photo #' . $photoId;
});
Router::get(
'/blog(/\d+)?(/\d+)?(/\d+)?(/[a-z0-9_-]+)?',
function($year = null, $month = null, $day = null, $slug = null) {
if (!$year) { echo 'Blog overview'; return; }
if (!$month) { echo 'Blog year overview'; return; }
if (!$day) { echo 'Blog month overview'; return; }
if (!$slug) { echo 'Blog day overview'; return; }
echo 'Blogpost ' . htmlentities($slug) . ' detail';
}
);
Router::get('/blog(/\d+(/\d+(/\d+(/[a-z0-9_-]+)?)?)?)?', function($year = null, $month = null, $day = null, $slug = null) {
// ...
});
Router::get('/blog(/\d{4}(/\d{2}(/\d{2}(/[a-z0-9_-]+)?)?)?)?', function($year = null, $month = null, $day = null, $slug = null) {
// ...
});
Router::mount('/movies', function() use ($router) {
// will result in '/movies/'
Router::get('/', function() {
echo 'movies overview';
});
// will result in '/movies/id'
Router::get('/(\d+)', function($id) {
echo 'movie id ' . htmlentities($id);
});
});
Router::get('/(\d+)', '\App\Controllers\User@showProfile');
Router::setNamespace('\App\Controllers');
Router::get('/users/(\d+)', 'User@showProfile');
Router::get('/cars/(\d+)', 'Car@showProfile');
Router::set404(function() {
header('HTTP/1.1 404 Not Found');
// ... do something special here
});
Router::set404('/api(/.*)?', function() {
header('HTTP/1.1 404 Not Found');
header('Content-Type: application/json');
$jsonArray = array();
$jsonArray['status'] = "404";
$jsonArray['status_text'] = "route not defined";
echo json_encode($jsonArray);
});
Router::set404('\App\Controllers\Error@notFound');
Router::get('/([a-z0-9-]+)', function($id) use ($router) {
if (!Posts::exists($id)) {
Router::trigger404();
return;
}
// …
});
Router::middleware('GET|POST', '/admin/.*', function() {
if (!isset($_SESSION['user'])) {
header('location: /auth/login');
exit();
}
});
Router::middleware('GET', '/.*', function() {
// ... this will always be executed
});
Router::run(function() { … });
Router::get('/', function() { echo 'Index'; });
Router::get('/hello', function() { echo 'Hello!'; });
// Override auto base path detection
Router::setBasePath('/');
Router::get('/', function() { echo 'Index'; });
Router::get('/hello', function() { echo 'Hello!'; });
Router::run();
$tpl = new \Acme\Template\Template();
Router::get('/', function() use ($tpl) {
$tpl->load('home.tpl');
$tpl->setdata(array(
'name' => 'Soanix!'
));
});
Router::run(function() use ($tpl) {
$tpl->display();
});
Router::put('/movies/(\d+)', function($id) {
// Fake $_PUT
$_PUT = array();
parse_str(file_get_contents('php://input'), $_PUT);
// ...
});