elliot
elliot

Reputation: 772

AuthComponent works great in development mode, but not in production?

I'm getting a bizarre error when using CakePHP's AuthComponent. As far as I can tell, there's nothing wrong with the code itself. It's working great on my development machine and the production server. However, if I change the 'debug' level in app/Core/config.php from 2 (development) to 0 (production) on the production server, the app fails with nothing but this in the error logs:

PHP Fatal error: Class 'AuthComponent' not found in /path/to/my/app/View/Elements/auth_status.ctp on line 3

I've checked that the file is present in lib/Cake/Controller/Component/AuthComponent.php. I've also experimentally added/removed App::uses('AuthComponent', 'Controller/Component') to AppController and my individual controllers to no avail. This one has me stumped, and I can't reproduce the error on my development machine. This seems to indicate a server issue, but I'm at a loss to find an explanation, and the docs aren't clear on the prerequisites required to run the AuthComponent. Any ideas how I can fix it? Thanks!

For reference, here's my AppController:

class AppController extends Controller {
  public $helpers = array('Recaptcha.Recaptcha');
  public $components = array(
    'Session',
    'Auth' => array(
        'loginAction' => array(
            'controller' => 'users',
            'action' => 'login'
        ),
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
  );

  public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow();
  }
}

The code for auth_status.ctp:

        <div class="pull-right">
        <?php if (AuthComponent::user('id')): ?>
            <?php echo $this->Html->link("Welcome!", '/users/view/'.AuthComponent::user('id'));?>
             | 
            <?php echo $this->Html->link('Logout', '/users/logout');?>
        <?php else: ?>
            <?php echo $this->Html->link('Login', '/users/login');?>
             | 
            <?php echo $this->Html->link('Register', '/users/register');?>
        <?php endif; ?>
        </div>

Upvotes: 3

Views: 1041

Answers (4)

Alex
Alex

Reputation: 31

I also ran into this problem and as it turned out my problem was, that I tried to load a plugin in bootstrap.php, but didn't have it installed

Upvotes: 0

elliot
elliot

Reputation: 772

I finally got to the bottom of this one. In core.php, there's a variable named $prefix that's used by the cache engine. If that engine is Memcache or APC, it must be changed to avoid naming collisions with any other Cake apps running on the server. My development laptop was using File cache engine, while the production server was using APC (and later Memcached). An older version of the same Cake app was running on that server and was not using AuthComponent. As soon as someone would make a request on that app, the other app would use the wrong cache and throw HTTP500 errors until I cleared the cache.

Change the $prefix variable to something unique to your app, and the problem disappears.

Upvotes: 3

BobuSumisu
BobuSumisu

Reputation: 118

I think this may be related to this issue. The error in my code was totally unrelated to AuthComponent (or Cake at all).

Unfortunately I can't know what the error is in your case - I had to to some real debugging before I pin-pointed my error.

If anyone wonders; my error was that I accessed functions as arrays and my production server was running a PHP version < 5.4.

Upvotes: 0

powtac
powtac

Reputation: 41080

You probably have different php.ini settings regarding include_path.

Check on development and on production server:

echo ini_get('include_path');

Upvotes: 0

Related Questions