Via is a simple and scalable router inspired by express.
This is the very first version, use with caution !
- PHP 5.3+
- (optional) URL rewriting
- Composer.
composer require bafs/via dev-master
composer install
<?php
use Via\Router;
require 'vendor/autoload.php';
$app = new 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));
});
Add a route with GET verb
Add a route with POST verb
Add a route with PUT verb
Add a route with DELETE verb
Add a route (can use muliple verb)
Add a route that always match
Add a route that always match and go to next
Add a namespace (prefix all routes)
Get value from container
Set value in app container
Render a view
$res->render("user.html", ["title" => "User"]);
In template we can use:<title><?=$title?></title>
$params
Parameters mapped from URL$query
Queries mapped from URL$cookies
Cookies$body
Proprieties from POST$ip
Get remote IP address$path
Get requested URL path$xhr
Check if request was done with 'XMLHttpRequest'$verb
HTTP verb (method)$url
Get current URL
Get a specific header
$req->header('host');
$req->header(...);
alias
Return if exists (in order) $req->params->$name, $req->body->$name or $req->query->$name
$req->param('str');
$req(...);
alias
Check if mime type is in header
$req->is('html');
Return path relative to root (eg. useful for assets in views)
Check if rewrite engine is active (rewrite can be enable but no active !)
Send output buffer
$res->send('text');
$res->send(404, 'Not found');
$res->send(new MyObj());
print json$res(...);
alias of$res->send(...);
Send output buffer and force json content-type
$res->json("{ user: 'Mario' }");
Same as send but encode special html chars
Redirect client to specific url
$res->redirect('http://cern.ch');
$res->redirect(301, '/test');
Force to download a specific file
$res->download('myImage.png');
$res->download('CV_en_v4.pdf', 'Smith_CV.pdf');
Read or write file
$res->file('myImage.png');
Read file$res->file('file.txt', $data);
Write data
$res->contentType('text/plain');
Set 'Content-Type' header to text/plain
Set header parameters
$res->set('Content-Type', 'text/plain');
Set 'Content-Type' header to text/plain-
'Content-Type' => 'text/plain', 'ETag' => '10000' ]);```
$res->status(404);
$res->cookie("TestCookie", "something", ["maxAge" => 3600, "secure" => true]);
$res->clearCookie("TestCookie");
Clear 'TestCookie' cookie
Alias of render in "Router"
To run the tests, go to the test folder, edit URL constant (run.php line 10) and run php run.php
.