我正在尝试使用以下命令运行 Silex 子请求
$subRequest = Request::create('/api/movie/show?id=61333');
$res = $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
当我在 Controller 中运行这两行时它工作得很好但是当我尝试在命令行中运行它时,在初始化路由和应用程序之后,我得到以下 PHP 错误:
PHP Fatal error: Uncaught exception 'RuntimeException' with message
'Accessed request service outside of request scope. Try moving that call to a before handler or controller.'
in /my/project/vendor/silex/silex/src/Silex/Application.php:132
我怀疑在命令行上下文中有什么错误/未初始化,但我无法弄清楚它是什么。有什么想法吗?
有关信息,我的命令行脚本看起来像
<?php
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
// service providers init ...
$app->register(new Provider\UrlGeneratorServiceProvider());
$app->register(new Provider\ServiceControllerServiceProvider());
// ... more service providers init
// routes definition
require_once __DIR__.'/../src/routes.php';
// run sub query
$subRequest = Request::create('/api/movie/show?id=61333');
$res = $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
提前致谢 (再一次,最后两行在 Controller 中工作正常)
[编辑]:由@smwhr 解决:我需要将 HttpKernelInterface::SUB_REQUEST 替换为 HttpKernelInterface::MASTER_REQUEST
最佳答案
您正在尝试在没有任何请求的情况下处理 SUBRequest。
试试这个:
$res = $app->handle($subRequest, HttpKernelInterface::MASTER_REQUEST, false);
如果您在脚本和 Controller 中使用相同的代码,只需测试 $app['request'] 的存在即可知道从中选择哪个
关于php - 在命令行中执行 SubRequest 时出现 RuntimeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21311715/