<p>I suspect not without manually manipulating the path before the Request class is initialised. </p>

<p>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 <code>getIsSiteRequest</code> set to <code>true</code>, for example:</p>

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

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

<pre><code>$response = $client-&gt;request('get', 'http://url/actions/plugin/controller/action');
</code></pre>

CC BY-SA 3.0
2018-04-25T07:13:02.507Z
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
Robin Schambach
CC BY-SA 3.0
2018-04-25T11:17:11.123Z
By hitting the front-end URL instead of a CP URL, `getIsSiteRequest` will automatically be set to true.
Ben Croker
CC BY-SA 3.0
2018-04-25T11:20:13.290Z
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
Robin Schambach
CC BY-SA 3.0
2018-04-25T16:07:41.517Z
I realise it's not ideal but I can't think of a way around it right now.
Ben Croker
CC BY-SA 3.0
2018-04-25T16:11:14.667Z
I added a second option to my answer which might be cleaner.
Ben Croker
CC BY-SA 3.0
2018-05-01T12:08:07.167Z
Apologies for the slow response, I wanted to wait until I'd dug into it a bit further before responding. I had a go at using Guzzle for the task last week. While it works for many cases, I ran into a few issues. It seems that in EVENT_AFTER_SAVE an entry that has just been published or enabled isn't actually enabled yet, so it returns a 404. I investigated using temporary tokens, but that doesn't seem like the optimal solution.
Jonathan
CC BY-SA 3.0
2018-05-01T15:32:16.267Z
Strange, I would expect the entry to be enabled by the time EVENT_AFTER_SAVE is triggered...
Ben Croker
CC BY-SA 3.0
2018-04-25T07:00:04.350Z
2018-04-25T16:12:40.023Z
2018-04-25T16:12:40.023Z
Ben Croker
<p>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:</p>

<p><code>if( Craft::$app-&gt;getRequest()-&gt;getIsSiteRequest() )</code></p>

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

<p><code>Craft::$app-&gt;getView()-&gt;setTemplateMode(View::TEMPLATE_MODE_SITE)</code></p>

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

<plugin-development><craft3>
Craft 3 – Render template as if it were a site request
# Craft 3 – Render template as if it were a site request

(Asked by: username_0 on 2018-04-23)

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:

`Craft::$app->getView()->setTemplateMode(View::TEMPLATE_MODE_SITE)`

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

**Tags:** plugin-development, craft3

---

## 1 answers

### Answer by username_1 on 2018-04-25. Score: 1

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

> **Comments:**
> **username_2**: 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  
> **username_1**: By hitting the front-end URL instead of a CP URL, `getIsSiteRequest` will automatically be set to true.  
> **username_2**: 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  
> **username_1**: I realise it's not ideal but I can't think of a way around it right now.  
> **username_1**: I added a second option to my answer which might be cleaner.  
> **username_0**: Apologies for the slow response, I wanted to wait until I'd dug into it a bit further before responding. I had a go at using Guzzle for the task last week. While it works for many cases, I ran into a few issues. It seems that in EVENT_AFTER_SAVE an entry that has just been published or enabled isn't actually enabled yet, so it returns a 404. I investigated using temporary tokens, but that doesn't seem like the optimal solution.  
> **username_1**: Strange, I would expect the entry to be enabled by the time EVENT_AFTER_SAVE is triggered...  

---

