CodeIgniter Forums

Full Version: Autoload cache drivers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
Good evening everyone,

I have a problem since a few days.

I would instantiate the Cache class, but I can't do this with the autoload; I've put this configuration file:
Code:
$autoload['libraries'] = array('driver', 'acl');
but I can't use it. In every class or model, I should write this
Code:
$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
can I use
Code:
$this->ci =& get_instance();
$this->ci->cache->get('foo');
like I do with other classes? Because at the moment I can't do this, even in a personal class, for example.

El Forum

[eluser]PhilTem[/eluser]
You can't load the cache driver multiple times. If you do

Code:
$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));

in your MY_Controller or the first library you will load it will be available to every library, model, helper loaded afterwards. Thus you don't need to repeat your code.

On another note: Yes, you can't autoload the cache driver, at least not to my knowledge - I tried it as well, didn't work.

El Forum

[eluser]Unknown[/eluser]
Why doesn't work?
Code:
class MY_Controller extends CI_Controller {

function __construct()
{
  parent::__construct();

  $this->load->driver('cache', array('adapter' => 'file', 'backup' => 'file'));
}
}

On this class:
Code:
class Acl {

private $ci;

function __construct()
{
  $this->ci =& get_instance();

  if (!$foo = $this->ci->cache->get('foo'))
  {
   echo 'Saving to the cache!<br />';
   $foo = 'foobarbaz!';

   // Save into the cache for 5 minutes
   $this->ci->cache->save('foo', $foo, 300);
  }

  echo $foo;
}
}
(Acl is loaded by autoload.php)

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$cache

Filename: libraries/Acl.php

Line Number: 13

Fatal error: Call to a member function get() on a non-object in application\libraries\Acl.php on line 13

El Forum

[eluser]tanero[/eluser]
[quote author="Federico Biccheddu" date="1351098462"]Why doesn't work?
Code:
class MY_Controller extends CI_Controller {

function __construct()
{
  parent::__construct();

  $this->load->driver('cache', array('adapter' => 'file', 'backup' => 'file'));
}
}

On this class:
Code:
class Acl {

private $ci;

function __construct()
{
  $this->ci =& get_instance();

  if (!$foo = $this->ci->cache->get('foo'))
  {
   echo 'Saving to the cache!<br />';
   $foo = 'foobarbaz!';

   // Save into the cache for 5 minutes
   $this->ci->cache->save('foo', $foo, 300);
  }

  echo $foo;
}
}
(Acl is loaded by autoload.php)

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$cache

Filename: libraries/Acl.php

Line Number: 13

Fatal error: Call to a member function get() on a non-object in application\libraries\Acl.php on line 13
[/quote]




You can not use MY_Controller class __construct function in your library, you just need to load cache driver in your Acl library.

El Forum

[eluser]Aken[/eluser]
Make sure your welcome controller extends MY_Controller, not CI_Controller.

By the way, autoloading drivers will be in CI 3.0.