PHP code example of bafs / via

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

    

bafs / via example snippets




use Via\Router;

$app->get('', function($req, $res) {
    $res('Hello world !');
});

$app(); // run the router

$app->get('/test', function($req, $res) {
	$res('Test OK'); // print 'Test OK'
	$res->send('Test OK'); // Same as above
});

$app->post('/test/:var', function($req, $res) {
	$res($req('var'));
	$res($req->params->var)); // Same as above
});

$app->on(['GET', 'POST'], '/test', function() {
	// ...
});

// Match both (print '12')
$app->get('/test', function($req, $res, $next) {
	$res('1');
	$next(); // Continue to test next routes
});

$app->get('/test', function($req, $res) {
	$res('2');
});

$app->with('/sub', function($app) {

	$app->with('/a', function($app) {
		// inside /sub/a

		$app->get('/test', function($req, $res){
			// match /sub/a/test
		});
	});
});

$app->get('/test', function($req, $res, $next) {
	// You can use $title inside view.html
	$html = $res->render('view.html', ['title' => 'My Title']);
	$res->send($html);
});

$app->using(function($req, $res) {
	$res->contentType('text/plain');
	$res->set('X-Custom-Header', 'test');
	// will continue to next route
});

// ...

class A {
	function b($req, $res) {...}
}

$app->get('/', 'A@b');

class MyController {
	function getUser($req, $res) {...}

	function postUser($req, $res) {...}
}

$app->get('/test/@@', 'MyController');

// GET /test/user will call "getUser"
// POST /test/user will call "postUser"

// GET /reg/F00D will print "F00D"
$app->get('R/reg/([0-9A-F]+)', function($req, $res, $next) {
	$res($req(0));
});
$res->set([
	  'Content-Type' => 'text/plain',
	  'ETag' => '10000'
	]);