Question

I'm trying to run a Silex sub request with the following commands

$subRequest = Request::create('/api/movie/show?id=61333');
$res = $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);

It works perfectly fine when I run these two lines within a Controller but when I try to run it within the command line, after initializing the routes and the app, I get the following PHP error :

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

I suspect there is something wrong/uninitialized in the command line context but I can't figure out what it is. Any idea ?

For information, my command line script looks like

<?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);

Thanks in advance (once again, the last two lines work fine within a Controller)

[Edit] : resolved by @smwhr : I needed to replace HttpKernelInterface::SUB_REQUEST with HttpKernelInterface::MASTER_REQUEST

Était-ce utile?

La solution

You are trying to handle a SUBrequest without having any request in the first place.

Try this :

$res = $app->handle($subRequest, HttpKernelInterface::MASTER_REQUEST, false);

In case you're using same code in script and controller, just test the existence of $app['request'] to know which to choose from

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top