• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 XenForo_Application::get('config')->cache->enabled

AndyB

Well-known member
#1
What would be the equivalent in XF2?

PHP:
$cacheEnabled = XenForo_Application::get('config')->cache->enabled;
Thank you.
 

Mike

XenForo developer
Staff member
#2
You'd just call the cache() method on the app object (a local $this->app or $this->app(), XF::app() if you have to). It returns null if there's no cache enabled.
 

AndyB

Well-known member
#3
If I add the following to my config.php:

PHP:
$config['cache']['enabled'] = true;
I would like to be able to see in my add-on if this has been entered into the config.php. If in my add-on I add the following:

PHP:
$cacheEnabled = \XF::app()->cache->enabled;
I get this error message:

ErrorException: Undefined property: Doctrine\Common\Cache\VoidCache::$enabled in src/addons/Andy/InsertAttachmentAll/Pub/Controller/InsertAttachmentAll.php at line 20

So what would be the correct PHP code to see if the cache is enabled?
 

Mike

XenForo developer
Staff member
#5
Just as a follow up, the existence of a cache object means that the cache is enabled. You don't check anything else.
 

AndyB

Well-known member
#6
Thank you, Fillip.

This worked perfect:

PHP:
$cacheObj = \XF::app()->cache();

Can you explain the purpose of this code.
PHP:
$cacheEnabled = $cacheObj ? $cacheObj->enabled : false;
 

Jake B.

Well-known member
#7
Thank you, Fillip.

This worked perfect:

PHP:
$cacheObj = \XF::app()->cache();

Can you explain the purpose of this code.
PHP:
$cacheEnabled = $cacheObj ? $cacheObj->enabled : false;
It's checking if the $cachObj->enabled is true and $cacheObj is a valid value
 

AndyB

Well-known member
#8
Thank you, Jake.

So I suppose this would be equivalent:

PHP:
        $cacheObj = \XF::app()->cache();
        
        if ($cacheObj)
        {
            
        }