我需要从Laravel框架之外的队列中推送作业。找到了应该实现这一目标的队列封面管理器:
https://github.com/illuminate/queue
根据示例代码尝试以下内容:
<?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();
});
运行此代码会导致以下错误:
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
搜索这个主题后,我发现了一些可以添加以下代码的内容:
$queue->getContainer()->bind('encrypter', function() {
return new Illuminate\Encryption\Encrypter('nInrMfTMQngxqRvoFpjstYjZX0qH1Nlr', 'AES-256-CBC');
});
$queue->getContainer()->bind('request', function() {
return new Illuminate\Http\Request();
});
这似乎不是一个解决方案,因为我得到以下错误:
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
从照明/队列的文档判断,不应该需要绑定那些。
这与laravel 5.1.27一起发生。有什么想法/解决方案吗?
更新:我在4.2环境中尝试过,在那里我还必须绑定加密器类,但这样做有效。 (不给出不可实例化的错误)
答案 0 :(得分:3)
在玩了一下后,你应该在加密器绑定之后再添加一个绑定来解决这个问题。
$queue->getContainer()->bind('Illuminate\Contracts\Encryption\Encrypter', 'encrypter');
将加密器绑定到合同。