请教一个laravel $app->singleton的问题
我自定义了一个类
class Custom
{
public function hello(?string $content = null ): void
{
echo $content;
}
}
我在服务提供者里将它注册后
public function register(): void
{
$this->app->singleton('custom',fn() => new Custom());
}
第一次调用是经过中间件调用,打印结果为null
第二次调用是中间件验证正常后,在控制器里使用
public function index()
{
app('custom')->hello('你好');
}
为啥结果还是null
?有大佬知道吗。我知道它返回的是同一个实例,但是我调用的同一个方法,参数不同。为什么同一次请求的第二次调用参数无效呢?
推荐文章: