Laravel 5.1:中间件终止函数使用的全局变量

I have written a middleware terminate function which would send all the logs collected during request execution to an external log server. These logs are collected mostly in helper functions which don't have direct access to request object and it would make my code ugly to send Request as parameter to helper functions when called from controllers.

Is there a clean way to have a global variable in Laravel 5.1 where I can store all the logs to be used later by terminate function?

If you want to use some kind of globally available message bag. You can do the following in your Service Provider register method:

$this->app->singleton('my.logging', function() {
    return new \Illuminate\Support\MessageBag;
});

Make sure your service provider is registered.

You can now call this in your middleware using:

$logBag = App::make('my.logging');

See the API documentation on how to use the message bag.