The Request Object - HL Vanilla Community
<main> <article class="userContent"> <div class="embedExternal embedImage display-large float-none"> <div class="embedExternal-content"> <a class="embedImage-link" href="https://us.v-cdn.net/6030677/uploads/DMZGSNDBFUZ1/microsoftteams-image-288-29.png" rel="nofollow noreferrer noopener ugc" target="_blank"> <img class="embedImage-img" src="https://us.v-cdn.net/6030677/uploads/DMZGSNDBFUZ1/microsoftteams-image-288-29.png" alt="MicrosoftTeams-image (8).png" height="108" width="1356" loading="lazy" data-display-size="large" data-float="none"></img></a> </div> </div> <p>Vanilla’s base Controller class (the class from which all <a href="https://docs.vanillaforums.com/developer/framework/controllers" rel="nofollow noreferrer ugc">controllers</a> are extended) accepts a DeliveryType parameter on every request. Depending on the type of delivery being requested, a controller in Garden will deliver all of a page, part of a page, or part of a page plus extra information as JSON.</p><h3 data-id="deliverytype">DeliveryType</h3><p>There are four delivery types available:</p><ol><li><code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_TYPE_ALL</code>: Entire page (default).</li><li><code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_TYPE_ASSET</code>: Content for the requested <a href="https://docs.vanillaforums.com/developer/framework/assets" rel="nofollow noreferrer ugc">asset</a>.</li><li><code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_TYPE_VIEW</code>: Only the requested <a href="https://docs.vanillaforums.com/developer/framework/views" rel="nofollow noreferrer ugc">view</a>.</li><li><code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_TYPE_BOOL</code>: Only report request success (true/false).</li></ol><p>If no delivery type is provided in the request, <code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_TYPE_ALL</code> is assumed.</p><p>In order to made the controller deliver content other than <code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_TYPE_ALL</code>, you would have to append the request URL (or post data) with the <code class="code codeInline" spellcheck="false" tabindex="0">DeliveryType</code> parameter set to the above value you want.</p><p>A <code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_TYPE_VIEW</code> request can be easily used for progressive data loading. That or a <code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_TYPE_ASSET</code> request could be used to update part of a page. A <code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_TYPE_BOOL</code> request is used for actions like closing a discussion.</p><h3 data-id="deliverymethod">DeliveryMethod</h3><p>The DeliveryMethod defines what format you want the response in.</p><ol><li><code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_METHOD_XHTML</code>: Delivered as HTML (default).</li><li><code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_METHOD_JSON</code>: As JSON.</li><li><code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_METHOD_XML</code>: As XML.</li><li><code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_METHOD_TEXT</code>: As text/plain (since 2.1).</li><li><code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_METHOD_PLAIN</code>: No Content-Type is set (since 2.1).</li><li><code class="code codeInline" spellcheck="false" tabindex="0">DELIVERY_METHOD_RSS</code>: An RSS feed (since 2.1).</li></ol><p>Vanilla builds datasets for its pages in generic collections that can be output in any of the above formats, assuming an appropriate view is available.</p><p>You can define a <code class="code codeInline" spellcheck="false" tabindex="0">DeliveryMethod</code> by appending it to your request like <code class="code codeInline" spellcheck="false" tabindex="0">DeliveryType</code>, or by appending an extension to your request after the method. For example, try viewing <code class="code codeInline" spellcheck="false" tabindex="0">/profile</code> on your forum vs. <code class="code codeInline" spellcheck="false" tabindex="0">/profile.json</code>. Note that arguments are added normally after another slash. Supposing you wanted to view userid 2’s profile with the name Lincoln, you would use <code class="code codeInline" spellcheck="false" tabindex="0">/profile.json/2/Lincoln</code>.</p><h3 data-id="parsing-requests">Parsing Requests</h3><p>Vanilla parses incoming URLs and parameters for you. There is rarely any reason to directly access PHP globals like <code class="code codeInline" spellcheck="false" tabindex="0">$_GET</code>. The <code class="code codeInline" spellcheck="false" tabindex="0">Gdn_Request</code> class (in <code class="code codeInline" spellcheck="false" tabindex="0">/library/core/class.request.php</code>) handles this for you.</p><p><code class="code codeInline" spellcheck="false" tabindex="0">Gdn::request()->domain()</code> will return the current domain. These methods also work as you’d expect: <code class="code codeInline" spellcheck="false" tabindex="0">host</code>, <code class="code codeInline" spellcheck="false" tabindex="0">ipAddress</code>, <code class="code codeInline" spellcheck="false" tabindex="0">path</code>, and <code class="code codeInline" spellcheck="false" tabindex="0">port</code>. You can also call <code class="code codeInline" spellcheck="false" tabindex="0">get</code> and <code class="code codeInline" spellcheck="false" tabindex="0">post</code> with a parameter name to see its current value. See <code class="code codeInline" spellcheck="false" tabindex="0">Gdn_Request</code> for more request data.</p><p>A few more useful methods:</p><p>Use <code class="code codeInline" spellcheck="false" tabindex="0">Gdn::request()->isAuthenticatedPostback()</code> to check that the current user sent a <code class="code codeInline" spellcheck="false" tabindex="0">POST</code> request along with their TransientKey to protect against CSRF attacks. All forms built with Vanilla include a TransientKey.</p><p>Use <code class="code codeInline" spellcheck="false" tabindex="0">Gdn::request()->url()</code> to build safe URLs to other parts of Vanilla. The <code class="code codeInline" spellcheck="false" tabindex="0">url()</code> function is a shortcut to this method.</p><p>Use <code class="code codeInline" spellcheck="false" tabindex="0">Gdn::request()->requestMethod()</code> to find out which Vanilla method was called by the current request.</p> </article> </main>