<p>If you have a URL like this:</p>

<pre><code>mysite.com?username=jsmith
</code></pre>

<p>You can retrieve it from your plugin via <code>craft()-&gt;request-&gt;getQuery('username');</code></p>

<p>See here: <a href="https://craftcms.com/classreference/services/HttpRequestService#getQuery-detail" rel="nofollow noreferrer">https://craftcms.com/classreference/services/HttpRequestService#getQuery-detail</a></p>

<blockquote>
  <p>My second question is - can Craft (Twig template) pass to the PHP plugin by including the PHP file (service)?</p>
</blockquote>

<p>I'm not actually following this question, but we generally try to keep to one question per post.  I'd suggest posting a new one for that with some additional details of exactly what you're trying to do.</p>

<p>Would also suggest giving the official plugin documentation a read-though: <a href="https://craftcms.com/docs/plugins/introduction" rel="nofollow noreferrer">https://craftcms.com/docs/plugins/introduction</a></p>

CC BY-SA 3.0
2018-01-25T21:43:14.987Z
2018-01-25T21:43:14.987Z
Brad Bell
<p>I'm pretty new to Craft and PHP, and I am needing to design a page in Craft (powered by a plugin) which will pass a variable in a querystring (or equivalent in Craft) to the PHP code in the plugin.<br>
In my case, it would be a username (e.g. jsmith) to pass to the plugin PHP code, which would query an XML file for a node having 'jsmith' as the username value.<br>
My first question is - in Craft, can I use a querystring like:
?username=jsmith</p>

<p>or would I need to use something more Craft-specific?</p>

<p>My second question is - can Craft (Twig template) pass to the PHP plugin by including the PHP file (service)?</p>

<p>Thanks for your help in this</p>

<plugin-development><plugins><php><craft3><variable>
Passing Querystring Variable to PHP Code in Plugin
# Passing Querystring Variable to PHP Code in Plugin

(Asked by: username_0 on 2018-01-25)

I'm pretty new to Craft and PHP, and I am needing to design a page in Craft (powered by a plugin) which will pass a variable in a querystring (or equivalent in Craft) to the PHP code in the plugin.  
In my case, it would be a username (e.g. jsmith) to pass to the plugin PHP code, which would query an XML file for a node having 'jsmith' as the username value.  
My first question is - in Craft, can I use a querystring like:
?username=jsmith

or would I need to use something more Craft-specific?

My second question is - can Craft (Twig template) pass to the PHP plugin by including the PHP file (service)?

Thanks for your help in this

**Tags:** plugin-development, plugins, php, craft3, variable

---

## 1 answers

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

If you have a URL like this:
mysite.com?username=jsmith

You can retrieve it from your plugin via craft()->request->getQuery('username');
See here: https://craftcms.com/classreference/services/HttpRequestService#getQuery-detail

My second question is - can Craft (Twig template) pass to the PHP plugin by including the PHP file (service)?

I'm not actually following this question, but we generally try to keep to one question per post.  I'd suggest posting a new one for that with some additional details of exactly what you're trying to do.
Would also suggest giving the official plugin documentation a read-though: https://craftcms.com/docs/plugins/introduction


---

