Skip to content

Instantly share code, notes, and snippets.

@Phunky
Last active August 29, 2015 14:27
Show Gist options
  • Save Phunky/06afff1c77b903b110b8 to your computer and use it in GitHub Desktop.
Save Phunky/06afff1c77b903b110b8 to your computer and use it in GitHub Desktop.
What is the best way to allow me to pass data down to my views for all routes via middleware
<?php
// Middleware for all requests
$app->add(function ($request, $response, $next) use ($container) {
if ($request->hasHeader('HTTP_X_PJAX')) {
// Here is where I would like to define an arg that can
// be passed down to my views for all requests
}
$response = $next($request, $response);
return $response;
});
$app->get('/{route}', function($request, $response, $args) {
$template = $args['route'] . '.twig';
// Goal of middleware is to replace done this in each route.
if ($request->hasHeader('HTTP_X_PJAX')) {
$args['isPjax'] = true;
}
return $this->view->render($response, $template, $args);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment