Skip to content
AzJezz edited this page Apr 22, 2018 · 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

This will create the middleware and run it immediately.

$tipsy->middleware(function($Request) {
	if ($Request->loc() == 'api') {
		// do special login stuff for any /api/* url
	}
});