<p>Ok, this is what I get for trying to be clever (lazy)... </p>

<p>I've always begrudged typing in '<strong>craft</strong>.myPlugin...' every time I wanted refer to my plugin, so I set a Route Variable in my plugin like so:</p>

<pre><code>  craft()-&gt;urlManager-&gt;setRouteVariables(
    array(
      'myPlugin' =&gt; craft()-&gt;myPlugin, 
    )
  );
</code></pre>

<p>It would appear none of my route variables work in the 404 template. That in itself is a bit odd, so to accomodate my laziness I've added this to the top of my 404 code:</p>

<pre><code>{% set myPlugin = craft.myPlugin %}
</code></pre>

<p>It would be interesting to know if this choice has any detrimental effects? <em>(besides being looked down upon by my peers)</em></p>

CC BY-SA 3.0
2015-10-06T09:09:17.910Z
I just found this discussion: http://craftcms.stackexchange.com/questions/4161/do-404s-get-access-to-variables-set-with-setroutevariables
carlcs
CC BY-SA 3.0
2015-10-06T09:09:42.617Z
Alternative idea: expose your plugin variables as [Twig globals](http://twig.sensiolabs.org/doc/advanced.html#globals).
carlcs
CC BY-SA 3.0
2015-10-06T08:35:10.033Z
2015-10-06T08:35:10.033Z
Mark Notton
<p>If you want to make a new variable available globally, you should do it by registering a new Twig extension, using the <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#addTwigExtension" rel="nofollow">addTwigExtension</a> hook. You can use the Twig extension’s <a href="http://twig.sensiolabs.org/api/master/Twig_Extension.html#method_getGlobals" rel="nofollow">getGlobals()</a> method to create whatever new global variables you want.</p>

CC BY-SA 3.0
2015-10-06T16:26:43.340Z
2015-10-06T16:26:43.340Z
Brandon Kelly
<p>Before I go into detail and share my error logs and plugin details, I'll ask the simple question first:</p>

<p><strong>When testing my 404 page, no plugin functionality works. Is this intentional?</strong> </p>

<p>Just for testing purposes, I tried to include the index template file in my 404 template:</p>

<pre><code>{% include 'index' %}
</code></pre>

<p>This pulls in the homepage fine, but still causes an error: </p>

<blockquote>
  <p>Variable "pluginName" does not exist in "404"...</p>
</blockquote>

<p>The following returns 'yes' when legitmately on the homepage, and 'no' when on a 404 page, which uses the <em>include</em> I just mentioned. </p>

<pre><code>{{ pluginName is defined ? 'yes' : 'no'}}
</code></pre>

<p>Any insight would be great. </p>

<p>Mark</p>

CC BY-SA 3.0
2015-10-06T07:06:41.500Z
There should not be any difference and your plugin / plugin variables should work on 404 just like on any other template. Does Twig / Craft work at all?
carlcs
CC BY-SA 3.0
2015-10-06T08:21:05.930Z
That's the weird thing. Craft/Twig functions work perfectly. {{ craft.request.getLastSegment() }} for example is fine. I'm going to give my plugin the once-over to see if there are any obvious issues.
Mark Notton
<plugin-development><404>
Plugin does not exist on 404 page?
# Plugin does not exist on 404 page?

(Asked by: username_0 on 2015-10-06)

Before I go into detail and share my error logs and plugin details, I'll ask the simple question first:
When testing my 404 page, no plugin functionality works. Is this intentional? 
Just for testing purposes, I tried to include the index template file in my 404 template:
{% include 'index' %}

This pulls in the homepage fine, but still causes an error: 

Variable "pluginName" does not exist in "404"...

The following returns 'yes' when legitmately on the homepage, and 'no' when on a 404 page, which uses the include I just mentioned. 
{{ pluginName is defined ? 'yes' : 'no'}}

Any insight would be great. 
Mark

**Tags:** plugin-development, 404
> **Comments:**
> **username_1**: There should not be any difference and your plugin / plugin variables should work on 404 just like on any other template. Does Twig / Craft work at all?  
> **username_0**: That's the weird thing. Craft/Twig functions work perfectly. {{ craft.request.getLastSegment() }} for example is fine. I'm going to give my plugin the once-over to see if there are any obvious issues.  

---

## 2 answers

### Answer by username_0 on 2015-10-06. Score: 2 | Accepted answer

Ok, this is what I get for trying to be clever (lazy)... 
I've always begrudged typing in 'craft.myPlugin...' every time I wanted refer to my plugin, so I set a Route Variable in my plugin like so:
  craft()->urlManager->setRouteVariables(
    array(
      'myPlugin' => craft()->myPlugin, 
    )
  );

It would appear none of my route variables work in the 404 template. That in itself is a bit odd, so to accomodate my laziness I've added this to the top of my 404 code:
{% set myPlugin = craft.myPlugin %}

It would be interesting to know if this choice has any detrimental effects? (besides being looked down upon by my peers)

> **Comments:**
> **username_1**: I just found this discussion: http://craftcms.stackexchange.com/questions/4161/do-404s-get-access-to-variables-set-with-setroutevariables  
> **username_1**: Alternative idea: expose your plugin variables as [Twig globals](http://twig.sensiolabs.org/doc/advanced.html#globals).  

---

### Answer by username_2 on 2015-10-06. Score: 2

If you want to make a new variable available globally, you should do it by registering a new Twig extension, using the [addTwigExtension](http://buildwithcraft.com/docs/plugins/hooks-reference#addTwigExtension) hook. You can use the Twig extension’s [getGlobals()](http://twig.sensiolabs.org/api/master/Twig_Extension.html#method_getGlobals) method to create whatever new global variables you want.


---

