如何实现超载关机功能?

发布于 2024-10-10 23:42:51 字数 447 浏览 2 评论 0原文

我正在使用 Kohana (v3) 框架,但我相信它与特定框架无关。

我所拥有的基本上是一个带有前端的应用程序,我想在其中使用 Kohana 的本机 Kohana::shutdown_handler(),但我也有一部分 - RESTful API - 但我不这样做”不需要彩色的 html 编码的异常报告。我想要一份纯文本报告。

我认为它可能工作的方式是在 API 的控制器抽象类构造函数中注册另一个关闭函数,但后来我意识到 register_shutdown_function() 的工作方式与 set_exception_handler() 不同,而不是替换它会为关闭程序添加另一个功能。更糟糕的是 PHP 不允许“取消注册”关闭函数,这就是我的问题所在。

如果您想使用另一个关闭功能而不是已经注册的功能,该怎么办?

I am using Kohana (v3) framework but I believe it's not tied to a particular framework.

What I have is basically an app with a front-end, where I want to use Kohana's native Kohana::shutdown_handler(), but I also have a part of the - RESTful API - where I don't want colourful and html-encoded exception reporting. I want a plain text reporting.

The way I thought it might work is to register another shutdown function in API's controller abstract class constructor, but then I realised register_shutdown_function() works differently to set_exception_handler() and instead of replacing it adds another function to the shutdown procedure. What's worse PHP doesn't allow "unregistering" shutdown functions and that's where my problem lies.

What to do, if you want to use another shutdown function instead of one already registered?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪心 2024-10-17 23:42:51

你可以简单地超载

views/kohana/error

by your custom view and set

/**
 * Initialize Kohana, setting the default options.
 *
 * The following options are available:
 *
 * - string   base_url    path, and optionally domain, of your application   NULL
 * - string   index_file  name of your index file, usually "index.php"       index.php
 * - string   charset     internal character set used for input and output   utf-8
 * - string   cache_dir   set the internal cache directory                   APPPATH/cache
 * - boolean  errors      enable or disable error handling                   TRUE
 * - boolean  profile     enable or disable internal profiling               TRUE
 * - boolean  caching     enable or disable internal caching                 FALSE
 */
Kohana::init(array(
 'base_url' => '/',
 'errors'   => FALSE,
));

You can simply overload

views/kohana/error

by your custom view and set

/**
 * Initialize Kohana, setting the default options.
 *
 * The following options are available:
 *
 * - string   base_url    path, and optionally domain, of your application   NULL
 * - string   index_file  name of your index file, usually "index.php"       index.php
 * - string   charset     internal character set used for input and output   utf-8
 * - string   cache_dir   set the internal cache directory                   APPPATH/cache
 * - boolean  errors      enable or disable error handling                   TRUE
 * - boolean  profile     enable or disable internal profiling               TRUE
 * - boolean  caching     enable or disable internal caching                 FALSE
 */
Kohana::init(array(
 'base_url' => '/',
 'errors'   => FALSE,
));
╭ゆ眷念 2024-10-17 23:42:51

首先想到的是在任何其他 register_shutdown_function() 调用之前添加您的函数,并让它调用任何必要的关闭函数,然后调用 exit 或 die 立即结束脚本,而不调用其他关闭功能。
为了确保您的调用是第一个,我会为其创建一个单独的文件并将其添加为 php.ini 的 auto_prepend_file
称之为黑客……也许吧。有效吗?我99%确信应该如此。

The first thing that comes to mind would be to add your function before any other register_shutdown_function() calls and have it call any necessary shutdown functions and then call exit or die to immediately end the script without calling other shutdown functions.
To ensure your call is first, I would make a separate file for it and add it as php.ini's auto_prepend_file.
Call it a hack...maybe. Does it work? I'm 99% sure it should.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文