Laravel 5 - 找不到应用程序::shutdown() 函数,是否有任何替代解决方案


Laravel 5 - Application::shutdown() function not found, is there any alternative solution?

得到致命错误:

调用未定义的方法 Illuminate''Foundation''Application::shutdown() in laravel 5

代码示例

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

使用 application::shutdown 方法注册一个"关闭"回调 在 laravel 5 中删除https://github.com/laravel/framework/commit/62ae860596f17a80954c106ff179288205a74d78

作为替代方法,您可以使用

1) register_shutdown_function php 原生函数

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

你需要工具

public function terminate($request, $response)

可终止中间件接口的函数。终止函数将在脚本结束时调用。

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

代码示例表单源

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

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

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

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

这可能是因为Laravel 5正在转向带注释的事件系统,也可能是因为Laravel 5中的临时开发故障。

相关文章: