dotelauv682684 2015-08-12 12:26
浏览 269
已采纳

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?

  • 写回答

1条回答 默认 最新

  • dqed19166 2015-08-12 13:19
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于c++外部库文件宏的问题,求解
  • ¥15 office打开卡退(新电脑重装office系统后)
  • ¥300 FLUENT 火箭发动机燃烧EDC仿真
  • ¥15 【Hadoop 问题】Hadoop编译所遇问题hadoop-common: make failed with error code 2
  • ¥15 vb6.0+webbrowser无法加载某个网页求解
  • ¥15 RPA财务机器人采购付款流程
  • ¥15 计算机图形多边形及三次样条曲线绘制
  • ¥15 根据protues画的图用keil写程序
  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误