Sorry if this is out of scope of the original question, but the above answers lead me to my solution of accessing previously-set properties in the terminate() method of my middleware, so I wanted to share what I did.
Using L5.5, I did not have to call $kernel->prependMiddleware() or $router->middleware() manually. I set my global middleware in the kernel.php along with all the other ones. Then in the AppServiceProvider I created a singleton of the middleware. The code starting with my middleware...
// Middleware.php
use My\Lovely;
class Middleware
{
protected $keepMeAround;
public function handle($request, \Closure $next, $guard = null)
{
$this->keepMeAround = "I'm here!";
return $next($request);
}
public function terminate($request, $response)
{
echo $this->keepMeAround; // I'm here!
}
}
And then the kernal...
// kernel.php
class Kernel extends HttpKernel
{
protected $middleware = [
My\Lovely\Middleware::class,
...
];
}
And finally the AppServiceProvider...
// AppServiceProvider.php
use My\Lovely\Middleware;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
// @see https://laravel.com/docs/5.5/middleware#terminable-middleware
//
// Manually add this as a singleton so that when the terminate() method
// is called on the middleware, we'll be using the same instance of
// the originally-instantiated My\Lovely\Middleware.
$this->app->singleton(Middleware::class, function ($app) {
return new Middleware();
});
}
}
If the instructions found at https://laravel.com/docs/5.5/middleware#terminable-middleware are confusing to you, hopefully this will save you a few minutes.
Cheers.