【问题标题】:Laravel 5 - Application::shutdown() function not found, is there any alternative solution?Laravel 5 - 找不到 Application::shutdown() 函数,有没有其他解决方案?
【发布时间】:2015-01-03 17:54:38
【问题描述】:

出现致命错误:

在 laravel 5 中调用未定义的方法 Illuminate\Foundation\Application::shutdown()

代码示例

App::shutdown(
    function () {
        // do somthing 
    }
);

【问题讨论】:

  • 没有任何代码你会得到这个错误?
  • 它可能对某人有所帮助。在较新的 Laravel 版本中,您可以使用 middlewareservice provider

标签: php laravel laravel-5


【解决方案1】:

使用 laravel 5 中移除的 Application::shutdown 方法注册一个“关闭”回调 https://github.com/laravel/framework/commit/62ae860596f17a80954c106ff179288205a74d78

你也可以使用

1) register_shutdown_functionphp原生函数

2) 使用 laravel 中间件并实现 Illuminate\Contracts\Routing\TerminableMiddleware 接口

你需要工具

public function terminate($request, $response)

TerminableMiddleware 接口的函数。 terminate 函数将在脚本结束时调用。

例如 laravel 使用 Illuminate\Session\Middleware\StartSession 类中的 TerminableMiddleware 接口在脚本末尾存储会话数据

代码示例表单源

public function terminate($request, $response)
{
    if ($this->sessionConfigured() && ! $this->usingCookieSessions())
    {
        $this->manager->driver()->save();
    }
}

【讨论】:

  • 谢谢,顺便说一句,这是TerminableInterface 而不是TerminableMiddleware
【解决方案2】:

Laravel 5 仍然是测试版软件,所以当/如果出现问题时不要感到惊讶。您可能会收到该错误,因为 while

Application::shutdown(function(){});

即使在 Laravel 4 中也是为关机设置事件处理程序的有效方法,它不是在 Laravel 5 中设置关机事件侦听器的有效方法。

这可能是因为 Laravel 5 正在向 annotated event system 移动,也可能是因为 Laravel 5 中的临时开发故障。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 2015-12-12
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多