PHP code example of ecoal95 / php-router

1. Go to this page and download the library: Download ecoal95/php-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/ */

    

ecoal95 / php-router example snippets


$router->url();

$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();
json
{
    "coal95/php-router": "dev-master"
    }
}
html
<a href=" echo $router->url('/users/username');