EllisLab text mark
Advanced Search
     
Autoload cache drivers
Posted: 24 October 2012 10:26 AM   [ Ignore ]
Avatar
Joined: 2012-08-11
2 posts

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:

$autoload['libraries'= array('driver''acl'); 

but I can’t use it. In every class or model, I should write this

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

can I use

$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.

 
Posted: 24 October 2012 10:48 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

You can’t load the cache driver multiple times. If you do

$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.

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 24 October 2012 01:07 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2012-08-11
2 posts

Why doesn’t work?

class MY_Controller extends CI_Controller {

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

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

On this class:

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'$foo300);
  
}

  
echo $foo
 
}

(Acl is loaded by autoload.php)

A PHP Error was encountered

Severity
Notice

Message
Undefined propertyWelcome::$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 
 
Posted: 05 November 2012 02:50 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2010-04-07
8 posts
Federico Biccheddu - 24 October 2012 01:07 PM

Why doesn’t work?

class MY_Controller extends CI_Controller {

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

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

On this class:

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'$foo300);
  
}

  
echo $foo
 
}

(Acl is loaded by autoload.php)

A PHP Error was encountered

Severity
Notice

Message
Undefined propertyWelcome::$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 

 


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

 

 
Posted: 06 November 2012 04:39 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

Make sure your welcome controller extends MY_Controller, not CI_Controller.

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