Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用第三方库flash作为提示,与fpm运行结果不一致 #48

Closed
Wqw9550 opened this issue Jun 7, 2018 · 3 comments
Closed
Labels
question Further information is requested

Comments

@Wqw9550
Copy link

Wqw9550 commented Jun 7, 2018

  1. Tell us your PHP version(php -v)

7.1.16

  1. Tell us your Swoole version(php --ri swoole)

4.0.0-beta

  1. Tell us your Laravel/Lumen version(check composer.json & composer.lock)

5.5.40

  1. Detail description about this issue(error/log)

使用第三方库flash做表单验证提醒,当第一次提醒成功时候有一次提醒,当第二次成功的时候有两个提醒,每次会增多,刷新都没用,而在fpm下运行不会出现此结果,该库的地址https://github.com/laracasts/flash,麻烦大神帮我看看
image

  1. Give us a reproducible code block and steps
// controller
flash("添加成功")->success()->important();
return view('test');

// view
@extends('layouts.app')
@section('content')
    @include('flash::message')
@endsection
@hhxsv5
Copy link
Owner

hhxsv5 commented Jun 7, 2018

  • 使用Swoole Http Server后,单例、全局变量、静态变量会被常驻内存,这是导致出现此问题的根本原因。而在FPM每次请求开始时初始化变量,请求完成后所有变量会自动回收。所以表现不一样。
    要解决这个问题,最直接有效的就是每次请求前重置请求完成后清理这些请求上下文
    具体参考单例问题

  • 具体到这里issue,因FlashServiceProvider中注册的是单例,所以此单例的成员$messages也被常驻内存,调用flash("添加成功")->success()->important();是往$messages中追加,故消息提醒会叠加。
    FlashNotifier.php#L95
    Collection.php#L1719

  • 最终解决方案有两个:

  1. 通过中间件在每次请求处理前请求完成后重置messages app('flash')->clear();
  2. 每次请求处理后重新注册FlashServiceProvider,参考配置register_providers

@hhxsv5 hhxsv5 added the question Further information is requested label Jun 7, 2018
@Wqw9550
Copy link
Author

Wqw9550 commented Jun 7, 2018

按照文档设置 register_providers 已经解决,谢谢

1 similar comment
@Wqw9550
Copy link
Author

Wqw9550 commented Jun 7, 2018

按照文档设置 register_providers 已经解决,谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants