Laravel 5.1 illuminate/queue capsule error 'Class encrypter does not exist'

- 1 answer

Ad

I have the need to push jobs on a queue from outside the Laravel framework. Found the queue capsule manager that should make this possible:

https://github.com/illuminate/queue

Trying the following as per their example code:

<?php

require_once __DIR__ . '/../../bootstrap/autoload.php';

use Illuminate\Queue\Capsule\Manager as Queue;

$queue = new Queue;

$queue->addConnection([
    'driver' => 'sync',
]);

$queue->setAsGlobal();

$queue->push(function($job)
{
    echo "test!\n";
    $job->delete();
});

Running this code results in the following error:

PHP Fatal error:  Uncaught exception 'ReflectionException' with message 'Class encrypter does not exist' in vendor/laravel/framework/src/Illuminate/Container/Container.php:741
Stack trace:
#0 vendor/laravel/framework/src/Illuminate/Container/Container.php(741): ReflectionClass->__construct('encrypter')
#1 vendor/laravel/framework/src/Illuminate/Container/Container.php(631): Illuminate\Container\Container->build('encrypter', Array)
#2 vendor/laravel/framework/src/Illuminate/Container/Container.php(1163): Illuminate\Container\Container->make('encrypter')
#3 vendor/laravel/framework/src/Illuminate/Queue/QueueManager.php(117): Illuminate\Container\Container->offsetGet('encrypter')
#4 vendor/laravel/framework/src/Illuminate/Queue/Capsule/Manager.php(135): Illuminate\Queue\QueueM in vendor/laravel/framework/src/Illuminate/Container/Container.php on line 741

Fatal error: Uncaught exception 'ReflectionException' with message 'Class encrypter does not exist' in vendor/laravel/framework/src/Illuminate/Container/Container.php:741
Stack trace:
#0 vendor/laravel/framework/src/Illuminate/Container/Container.php(741): ReflectionClass->__construct('encrypter')
#1 vendor/laravel/framework/src/Illuminate/Container/Container.php(631): Illuminate\Container\Container->build('encrypter', Array)
#2 vendor/laravel/framework/src/Illuminate/Container/Container.php(1163): Illuminate\Container\Container->make('encrypter')
#3 vendor/laravel/framework/src/Illuminate/Queue/QueueManager.php(117): Illuminate\Container\Container->offsetGet('encrypter')
#4 vendor/laravel/framework/src/Illuminate/Queue/Capsule/Manager.php(135): Illuminate\Queue\QueueM in vendor/laravel/framework/src/Illuminate/Container/Container.php on line 741

Searching on this subject I found something that said to add the following code:

$queue->getContainer()->bind('encrypter', function() {
    return new Illuminate\Encryption\Encrypter('nInrMfTMQngxqRvoFpjstYjZX0qH1Nlr', 'AES-256-CBC');
});
$queue->getContainer()->bind('request', function() {
    return new Illuminate\Http\Request();
});

Which does not seem to be a solution, because I then get the following error:

PHP Fatal error:  Uncaught exception 'Illuminate\Contracts\Container\BindingResolutionException' with message 'Target [Illuminate\Contracts\Encryption\Encrypter] is not instantiable.' in vendor/laravel/framework/src/Illuminate/Container/Container.php:749
Stack trace:
#0 vendor/laravel/framework/src/Illuminate/Container/Container.php(631): Illuminate\Container\Container->build('Illuminate\\Cont...', Array)
#1 vendor/laravel/framework/src/Illuminate/Container/Container.php(842): Illuminate\Container\Container->make('Illuminate\\Cont...')
#2 vendor/laravel/framework/src/Illuminate/Container/Container.php(805): Illuminate\Container\Container->resolveClass(Object(ReflectionParameter))
#3 vendor/laravel/framework/src/Illuminate/Container/Container.php(776): Illuminate\Container\Container->getDependencies(Array, Array)
#4  in vendor/laravel/framework/src/Illuminate/Container/Container.php on line 749

Binding those should not be needed though, judging by the documentation from illuminate/queue.

This occurs with laravel 5.1.27. Any thoughts/solutions?

Update: I have tried it in a 4.2 environment, there I also have to bind the encrypter class, but doing that there works. (Does not give the not instantiable error)

Ad

Answer

Ad

After playing around a bit you should add one more binding, after the encrypter binding, to get around this issue.

$queue->getContainer()->bind('Illuminate\Contracts\Encryption\Encrypter', 'encrypter');

Binding the encrypter to the contract.

Ad
source: stackoverflow.com
Ad