提问人:Aaryn 提问时间:10/4/2022 最后编辑:Aaryn 更新时间:10/4/2022 访问量:91
Silex 2.3 和 Symfony 4.4 - 未捕获错误:调用成员函数 has() on null
Silex 2.3 and Symfony 4.4 - Uncaught Error: Call to a member function has() on null
问:
我们有一个旧的但非常大的 Silex 应用程序,使用 Silex 2.2.5 和 Symfony 3.4 的分支。我们打算从 Silex 迁移出去,但该应用程序仍在由多个工程师进行大量开发,因此与此同时,我们希望在 22 年 11 月 7.4 EOL 之前进入 PHP 8.1。我们发现我们实际上并没有使用Silex的最终版本(2.3.0),它需要Symfony 4,而Symfony 4.4支持PHP 8.1。
我们已经升级了我们的依赖项以适应 Silex 2.3.0,那里没有问题。所有 Symfony 组件都升级到了 4.4。但是当我们在应用程序中加载页面时,我们收到以下错误:
PHP 致命错误:未捕获错误:调用成员函数 has() 在 /var/task/vendor/symfony/http-kernel/EventListener/SessionListener.php:41 中为 null
这源于一个名为 的方法。这是在 Symfony 4.4 中引入的。调试页面加载,似乎是空的。我还发现,在遇到此错误之前,没有调用SessionListener的构造函数。onKernelRequest
$this->container
class SessionListener extends AbstractSessionListener
{
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function onKernelRequest(GetResponseEvent $event)
{
parent::onKernelRequest($event);
// ERROR IS HIT ON THE FOLLOWING LINE
if (!$event->isMasterRequest() || !$this->container->has('session')) {
return;
}
if ($this->container->has('session_storage')
&& ($storage = $this->container->get('session_storage')) instanceof NativeSessionStorage
&& ($masterRequest = $this->container->get('request_stack')->getMasterRequest())
&& $masterRequest->isSecure()
) {
$storage->setOptions(['cookie_secure' => true]);
}
}
编辑:有趣的是,如果我从 Symfony 的 SessionListener
中删除这个 onKernelRequest
方法,该网站似乎可以正常工作。并不是说这是一个解决方案,因为它在 Symonfy 库中。
我已经搜索了 Silex 上留下的文档,看看 2.3.0 是否需要更多的东西才能与 Symfony 4 一起使用,但由于 Symfony 4.3 及更低版本没有这种方法,也许他们让 Silex 使用早期版本的 Symfony 4。
在谷歌上搜索错误,我确实看到一些关于用services.yml做一些事情来注入容器依赖项的喋喋不休。但是这个项目没有services.yml文件,所以我不确定它的起点是什么。
不指望有人对这个问题有答案,但想法?有没有人曾经让 Silex 2.3.0 与 Symfony 4.4 一起工作?
错误的完整堆栈跟踪:
result = {array} [9]
0 = {array} [7]
file = "/var/task/vendor/symfony/event-dispatcher/EventDispatcher.php"
line = {int} 264
function = "onKernelRequest"
class = "Symfony\Component\HttpKernel\EventListener\SessionListener"
object = {OurCompany\Session\SessionListener} [3]
type = "->"
args = {array} [3]
1 = {array} [7]
file = "/var/task/vendor/symfony/event-dispatcher/EventDispatcher.php"
line = {int} 239
function = "doDispatch"
class = "Symfony\Component\EventDispatcher\EventDispatcher"
object = {Symfony\Component\EventDispatcher\EventDispatcher} [3]
type = "->"
args = {array} [3]
2 = {array} [7]
file = "/var/task/vendor/symfony/event-dispatcher/EventDispatcher.php"
line = {int} 73
function = "callListeners"
class = "Symfony\Component\EventDispatcher\EventDispatcher"
object = {Symfony\Component\EventDispatcher\EventDispatcher} [3]
type = "->"
args = {array} [3]
3 = {array} [7]
file = "/var/task/vendor/symfony/http-kernel/HttpKernel.php"
line = {int} 135
function = "dispatch"
class = "Symfony\Component\EventDispatcher\EventDispatcher"
object = {Symfony\Component\EventDispatcher\EventDispatcher} [3]
type = "->"
args = {array} [2]
4 = {array} [7]
file = "/var/task/vendor/symfony/http-kernel/HttpKernel.php"
line = {int} 81
function = "handleRaw"
class = "Symfony\Component\HttpKernel\HttpKernel"
object = {Symfony\Component\HttpKernel\HttpKernel} [4]
type = "->"
args = {array} [2]
5 = {array} [7]
file = "/var/task/vendor/silex/silex/src/Silex/Application.php"
line = {int} 496
function = "handle"
class = "Symfony\Component\HttpKernel\HttpKernel"
object = {Symfony\Component\HttpKernel\HttpKernel} [4]
type = "->"
args = {array} [3]
6 = {array} [7]
file = "/var/task/vendor/silex/silex/src/Silex/Application.php"
line = {int} 477
function = "handle"
class = "Silex\Application"
object = {OurCompany\Application} [9]
type = "->"
args = {array} [1]
7 = {array} [7]
file = "/var/task/lib/Application.php"
line = {int} 37
function = "run"
class = "Silex\Application"
object = {OurCompany\Application} [9]
type = "->"
args = {array} [1]
8 = {array} [7]
file = "/var/task/web/index.php"
line = {int} 43
function = "run"
class = "OurCompany\Application"
object = {OurCompany\Application} [9]
type = "->"
args = {array} [1]
答:
无法再注入 ContainerInterface。 正如我所看到的,您想要读取会话数据。为此,您可以注入 SessionInterface(用于 Symfony 4)或 RequestStack(用于 Symfony 5+)而不是 ContainerInterface。
Symfony 4 : https://symfony.com/doc/4.4/session.html Symfony 5+ : https://symfony.com/doc/5.4/session.html
评论