PHP code example of marceauka / router

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

    

marceauka / router example snippets


$router = new Router;

// Will respond to : /hello (en GET)
$router->get('hello', function () {
	echo 'Hello world!';
});

// POST request
$router->post(...);
// PATCH request
$router->patch(...);

// GET and POST requests
$router->add(['GET', 'POST'], 'hello', ...);
// ... Or all HTTP verbs
$router->any('hello', ...);

// Chaining
$router->get('foo', ...)->get('bar', ...);
// ...or via a callback
$router->routes(function ($router) {
	// Votre logique
});

$router->get('hello', ...);

var_dump($router->routes()); // Returns [1 => ['uri' => 'hello', 'action' => '...']]

// Will use REQUEST_URI and REQUEST_METHOD
$router->listen();
// You can spoof them with your own logic (Request library for example).
$router->listing('request', 'method');

$router->get('hello', function () {
	echo 'Hello!';
})

$router->get('hello', 'MyClass@myMethod');

$router->get('route-namespace', 'App\Http\Controller@myMethod');

// Will call App\Http\Controller\Account@resume()
$router->namespaceWith('App\Http\Controller')->get('mon-account', 'Account@resume');

$router->whenNotFound(function () {
    echo 'Page not found';
});

// Autorise "profile/1" or "profile/12" but not "profile/john".
$router->get('profile/{:num}', ...);

$router->get('profile/{:num}/{:alpha}', function ($id, $name) {
	echo "My name is $name and my ID is $id !";
});

$router->get('/homepage', '...', 'home');
$router->link('home'); // Returns "/homepage"

$router->get('/tag/{:slug}', '...', 'tag');
$router->link('tag', 'wordpress'); // => Returns "/tag/wordpress"

$router->get('/user/{:num}/{:any}', '...', 'profile');
$router->link('profile', [42, 'JohnDoe']); // => Returns "/user/42/JohnDoe"

$compiled = $router->getCompiled(); // Retourns a string

$router = MarceauKa\Router::fromCompiled($compiled); // Returns the instance previously configured