ecoal95 / php-router
Minimal routing library
Installs: 1 018
Dependents: 1
Suggesters: 0
Security: 0
Stars: 27
Watchers: 7
Forks: 12
pkg:composer/ecoal95/php-router
Requires
- php: >=5.3.0
Requires (Dev)
- ext-xdebug: *
- phpdocumentor/phpdocumentor: 2.8.*
- phpunit/phpunit: 4.8.*
- squizlabs/php_codesniffer: 2.*
This package is not auto-updated.
Last update: 2025-10-26 00:28:33 UTC
README
Contributors
Composer setup example
{
"require": {
"ecoal95/php-router": "dev-master"
}
}
Usage
See the examples/ to see a basic example and remember to use the htaccess provided there!
Get urls
<a href="<?php echo $router->url('/users/username'); ?>">Username's profile</a>
Get the current url
$router->url();
Blog example
$router = new Router\Router('/blog'); $router->add('/', function() { echo 'homepage'; }); $router->add('/about', function() { echo 'about page'; }); $router->add('/([0-9]{4})', function($year) { echo $year . ' years active'; }); $router->add('/([0-9]{4})/([0-9]{2})', function ($year, $month) { echo 'archives from ' . $year . '/' . $month; }); $router->add('/(.*)', function($slug) { // For example: if( Article::where('slug', '=', $slug)->first() ) { echo 'article'; } else { echo '404'; } }); $router->post('/posts/create', function() { echo 'post created'; }); $router->post('/posts/update/([0-9]+)', function($post_id){ // Update post with id = $post_id }); // compare url with all registered routes $router->route();