PHP code example of rareloop / wp-router

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

    

rareloop / wp-router example snippets


use Rareloop\WordPress\Router\Router;

// Creates a route that matches the uri `/posts/list` both GET 
// and POST requests. 
Router::map(['GET', 'POST'], 'posts/list', function () {
    return 'Hello World';
});

Router::map(['GET'], 'posts/{id}', function(RouteParams $params) {
    return $params->id;
});

Router::map(['GET'], 'posts/all', function () {})->name('posts.index');

$url = Router::url('posts.index');

Router::map(['GET'], 'posts/{id}', function () {})->name('posts.show');

$url = Router::url('posts.show', ['id' => 123]);

Router::get('test/route', function () {});
Router::post('test/route', function () {});
Router::put('test/route', function () {});
Router::patch('test/route', function () {});
Router::delete('test/route', function () {});
Router::options('test/route', function () {});

Router::setBasePath('base/path');
Router::map(['GET'], 'route/uri', function () {}); // `/base/path/route/uri`

// TestController.php
namespace \MyNamespace;

class TestController
{
    public function testMethod()
    {
        return 'Hello World';
    }
}

// routes.php
Router::map(['GET'], 'route/uri', '\MyNamespace\TestController@testMethod');

Router::group('prefix', function ($group) {
    $group->map(['GET'], 'route1', function () {}); // `/prefix/route1`
    $group->map(['GET'], 'route2', function () {}); // `/prefix/route2§`
});