I am trying to get started with PhpRedis in Laravel 5.4 and in this link: https://github.com/laravel/framework/commit/1a1969b6e6f793c3b2a479362641487ee9cbf736 it says to change share() function to singleton() as share is no more supported in Laravel 5.4.
By default (in the process of installing redis) I get this error:
PHP Fatal error: Call to undefined method Illuminate\Foundation\Application::share() in ...\vendor\vetruvet\laravel-phpredis\src\Vetruvet\PhpRedis\PhpRedisServiceProvider.php on line 12
If I change share() to singledton() in PhpRedisServiceProvider.php I get error:
Error Exception Illegal offset type in unset
If changing share to singleton is correct then how to fix illegal offset?
New Updated error
PHP Fatal error: Class 'Illuminate\Redis\Database' not found in
...\vendor\vetruvet\laravel-phpredis\src\Vetruvet\PhpRedis\Database.php on line 7
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Illuminate\Redis\Database' not found
Composer.json
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0",
"laravelcollective/html": "~5.0",
"laracasts/utilities": "~2.0",
"guzzlehttp/guzzle": "~5.3|~6.0",
"vetruvet/laravel-phpredis": "1.*"
},
app.php
Illuminate\Queue\QueueServiceProvider::class,
//Illuminate\Redis\RedisServiceProvider::class,
Vetruvet\PhpRedis\PhpRedisServiceProvider::class,
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Aliases in app.php
//'Redis' => Illuminate\Support\Facades\Redis::class,
'LRedis' => Illuminate\Support\Facades\Redis::class,
PhpRedisServiceProvider.php
<?php
namespace Vetruvet\PhpRedis;
use Illuminate\Support\ServiceProvider;
class PhpRedisServiceProvider extends ServiceProvider {
protected $defer = true;
public function register() {
//$this->app['redis'] = $this->app->share(function($app) {
$this->app->singleton('redis', function ($app) {
return new Database($app['config']['database.redis']);
});
}
public function provides() {
return array('redis');
}
}
PhpRedis\Database.php
<?php
namespace Vetruvet\PhpRedis;
use \Redis;
class Database extends \Illuminate\Redis\Database {
/**
* Create a new aggregate client supporting sharding.
*
* @param array $servers
* @return array
*/
protected function createAggregateClient(array $servers) {
$options = array(
'lazy_connect' => true,
'pconnect' => false,
'timeout' => 0,
);
$cluster = array();
foreach ($servers as $key => $server) {
if ($key === 'cluster') continue;
$host = empty($server['host']) ? '127.0.0.1' : $server['host'];
$port = empty($server['port']) ? '6379' : $server['port'];
$serializer = Redis::SERIALIZER_NONE;
if (!empty($server['serializer'])) {
if ($server['serializer'] === 'none') {
$serializer = Redis::SERIALIZER_PHP;
} else if ($server['serializer'] === 'igbinary') {
if (defined('Redis::SERIALIZER_IGBINARY')) {
$serializer = Redis::SERIALIZER_IGBINARY;
} else {
$serializer = Redis::SERIALIZER_PHP;
}
}
}
$cluster[$host.':'.$port] = array(
'prefix' => empty($server['prefix']) ? '' : $server['prefix'],
'database' => empty($server['database']) ? 0 : $server['database'],
'serializer' => $serializer,
);
if (isset($server['persistent'])) {
$options['pconnect'] = $options['pconnect'] && $server['persistent'];
} else {
$options['pconnect'] = false;
}
if (!empty($server['timeout'])) {
$options['timeout'] = max($options['timeout'], $server['timeout']);
}
}
$ra = new RedisArray(array_keys($cluster), $options);
foreach ($cluster as $host => $options) {
$redis = $ra->_instance($host);
$redis->setOption(Redis::OPT_PREFIX, $options['prefix']);
$redis->setOption(Redis::OPT_SERIALIZER, $options['serializer']);
$redis->select($options['database']);
}
return array('default' => $ra);
}
/**
* Create an array of single connection clients.
*
* @param array $servers
* @return array
*/
protected function createSingleClients(array $servers) {
$clients = array();
foreach ($servers as $key => $server) {
if ($key === 'cluster') continue;
$redis = new Redis();
$host = empty($server['host']) ? '127.0.0.1' : $server['host'];
$port = empty($server['port']) ? '6379' : $server['port'];
$timeout = empty($server['timeout']) ? 0 : $server['timeout'];
if (isset($server['persistent']) && $server['persistent']) {
$redis->pconnect($host, $port, $timeout);
} else {
$redis->connect($host, $port, $timeout);
}
if (!empty($server['prefix'])) {
$redis->setOption(Redis::OPT_PREFIX, $server['prefix']);
}
if (!empty($server['database'])) {
$redis->select($server['database']);
}
if (!empty($server['serializer'])) {
$serializer = Redis::SERIALIZER_NONE;
if ($server['serializer'] === 'php') {
$serializer = Redis::SERIALIZER_PHP;
} else if ($server['serializer'] === 'igbinary') {
if (defined('Redis::SERIALIZER_IGBINARY')) {
$serializer = Redis::SERIALIZER_IGBINARY;
} else {
$serializer = Redis::SERIALIZER_PHP;
}
}
$redis->setOption(Redis::OPT_SERIALIZER, $serializer);
}
$clients[$key] = $redis;
}
return $clients;
}
}