Laravel/Octane:如何重置路由控制器?状态

发布于 2025-01-09 01:43:34 字数 418 浏览 6 评论 0原文

在 Laravel v9/Octane/Swoole 中,我在路由控制器中确实有私有属性,例如

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;

class SignupController extends Controller
{
    /** @var ?\App\SignupCode A verification code object */
    protected $code;

,看起来该属性在 Octane 下的请求之间“共享”。我有更多这样的控制器。如何确保控制器状态在每次请求时都会重置?我已经阅读了整个 Octane 文档几次,但仍然不清楚如何做到这一点。

In Laravel v9/Octane/Swoole, I do have private properties in route controllers, e.g.

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;

class SignupController extends Controller
{
    /** @var ?\App\SignupCode A verification code object */
    protected $code;

It looks like the property is "shared" between requests under Octane. I have more controllers like this. How do I make sure the controller state gets reset on every request? I've read the whole Octane documentation a few times, and it's still unclear how to do that.

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

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

发布评论

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

评论(1

不再让梦枯萎 2025-01-16 01:43:34

我通过创建侦听器解决了这个问题

<?php

namespace App\Listeners;

use Illuminate\Routing\Router;

class ResetControllerState
{
    /**
     * Handle the event.
     *
     * @param  mixed  $event
     * @return void
     */
    public function handle($event): void
    {
        /** @var Router $router */
        $router = $event->sandbox->make(Router::class);

        $currentRoute = $router->current();

        if($currentRoute && $currentRoute->controller)
            $currentRoute->controller = null;

    }
}

,并将其添加到辛烷配置中的侦听器数组中,

RequestReceived::class => [
    ...Octane::prepareApplicationForNextOperation(),
    ...Octane::prepareApplicationForNextRequest(),
    \App\Listeners\ResetControllerState::class
    //
],

我不知道可能会产生什么后果,但到目前为止它运行良好。

I solved it by created listener

<?php

namespace App\Listeners;

use Illuminate\Routing\Router;

class ResetControllerState
{
    /**
     * Handle the event.
     *
     * @param  mixed  $event
     * @return void
     */
    public function handle($event): void
    {
        /** @var Router $router */
        $router = $event->sandbox->make(Router::class);

        $currentRoute = $router->current();

        if($currentRoute && $currentRoute->controller)
            $currentRoute->controller = null;

    }
}

and add it to array of listeners in octane config

RequestReceived::class => [
    ...Octane::prepareApplicationForNextOperation(),
    ...Octane::prepareApplicationForNextRequest(),
    \App\Listeners\ResetControllerState::class
    //
],

I do not know what the consequences may be, but so far it works well.

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