Skip to content
Devin Smith edited this page Dec 6, 2015 · 10 revisions

Middleware allows you to intercept or execute services before the app runs. Use the run function as you would construct. Middleware supports the same definitions as Services.

Login middleware example

$tipsy = new Tipsy\Tipsy;

$tipsy->middleware('LoginService', [
	run => function() {
		if (!$_SESSION['user']) {
			// login
		} else {
			$this->user = 'devin';
		}
	}
]);

$tipsy->router()->home(function($LoginService) {
	echo $LoginService->user;
});

$tipsy->run();

A middleware closure using dependency injection

$tipsy->middleware(function($Request) {
	if ($Request->loc() == 'logout') {
		// do stuff
	}
});