I have a Craft 3 plugin that renders a portion of an entry after save in order to index the resulting information. The plugin needs full access to the Twig extensions on the site to render the template, however several plugins that add such extensions check:

if( Craft::$app->getRequest()->getIsSiteRequest() )

As a result not all Twig extensions load when renderTemplate is called from the control panel. I set the template mode using:


but that only updates the path to the template. Is it possible to render the template as if it were a site request?


I suspect not without manually manipulating the path before the Request class is initialised.

One possible alternative solution would be to use Guzzle to call the front-end URL rather than the CP URL, which will render the template with getIsSiteRequest set to true, for example:

$client = new \GuzzleHttp\Client();
$response = $client->request('get', 'http://url/front-end-template');
$body = $response->getBody();

It would also be possible to create your own controller which fetches the template and to hit it with a front-end action request:

$response = $client->request('get', 'http://url/actions/plugin/controller/action');
  • How should this enable one to manipulate the getIsSiteRequest to become true? The variable is set during the initialization of the component craft\web\Request and it checks the current URL segments for certain strings once. After that it returns always the same value no matter what. The only way I found is to change the classloader in order to load a custom Request class Apr 25 '18 at 7:13
  • By hitting the front-end URL instead of a CP URL, getIsSiteRequest will automatically be set to true.
    – Ben Croker
    Apr 25 '18 at 11:17
  • Ahh alright I missunderstood you. I thought you wanted to hit the request in order to change it on global level.. Of course you are totally right. Sorry for the confusion Apr 25 '18 at 11:20
  • I realise it's not ideal but I can't think of a way around it right now.
    – Ben Croker
    Apr 25 '18 at 16:07
  • I added a second option to my answer which might be cleaner.
    – Ben Croker
    Apr 25 '18 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.