How can I force a request type (HttpRequestService::isCpRequest vs. HttpRequestService::isSiteRequest)? Is there a way to force a request type (HttpRequestService::isCpRequest vs. HttpRequestService::isSiteRequest)?

In my case I'm generating some indexing data on entries.onSaveEntry. I'm trying to use some UrlHelper methods, but the resulting urls are wrong, since it thinks it is a cp request.

I have to do something like this load templates:

craft()-&gt;templates-&gt;setTemplateMode(TemplateMode::Site);


Is there any way to do something similar for request type?
There's no setRequestType() method or the like, no. The HttpRequestService will consider any request with the cpTrigger in its URL a control panel request, and I don't see any easy way around that.

It's a bit convoluted, but what you could do is to wrap whatever code you're currently executing in your entries.onSaveEntry in a controller action method, and then use Guzzle in your onSaveEntry event handler to trigger a site request (i.e. a request without the cpTrigger in the URL) to that action.

Here's how your controller action could look:

&lt;?
namespace Craft;

class MyPluginController extends BaseController
{

    protected $allowAnonymous = ['actionGenerateIndex'];

    public function actionGenerateIndex ()
    {

        // Get entry (based on a ?entryId URL parameter
        $entryId = craft()-&gt;request-&gt;getParam('entryId');
        $entry = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry, [
            'id' =&gt; $entryId,
        ])-&gt;first();

        // ...your code to generate the index data here

    }
}


Here's how you could fire off a request to this action using Guzzle, stripping the cpTrigger from the action URL to make sure Craft treats the action as a site request:

craft()-&gt;on('entries.onSaveEntry', function (Event $e) {

    // Get entry
    $entry = $e-&gt;params['entry'];

    // Get action URL, strip the `cpTrigger` segment
    $actionUrl = UrlHelper::getActionUrl('myPlugin/generateIndex', [
        'entryId' =&gt; $entry-&gt;id,
    ]);
    $actionUrl = str_replace(craft()-&gt;config-&gt;get('cpTrigger').'/', '', $actionUrl);

    // Request action
    $client = new Guzzle\Http\Client();
    $request = $client-&gt;get($actionUrl, [], [
      'headers' =&gt; [
        'X-Requested-With' =&gt; 'XMLHttpRequest',
      ],
    ]);
    $request-&gt;send();

});