PhpRedis错误异常unset中的非法偏移类型或Php致命错误php调用未定义方法share() - Laravel 5.4

时间:2017-04-18 12:05:50

标签: php laravel laravel-5.4 phpredis

我正在尝试在Laravel 5.4和这个链接中开始使用PhpRedis:https://github.com/laravel/framework/commit/1a1969b6e6f793c3b2a479362641487ee9cbf736它说要将share()函数更改为singleton(),因为Laravel 5.4不再支持共享。

默认情况下(在安装redis的过程中)我收到此错误:

PHP Fatal error:  Call to undefined method Illuminate\Foundation\Application::share() in ...\vendor\vetruvet\laravel-phpredis\src\Vetruvet\PhpRedis\PhpRedisServiceProvider.php on line 12

如果我在PhpRedisServiceProvider.php中将share()更改为singledton(),我会收到错误:

Error Exception Illegal offset type in unset

如果将份额更改为单身是正确的,那么如何修复非法偏移?

新更新错误

PHP致命错误:

中找不到类'Illuminate \ Redis \ Database'
...\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,

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;
    }
}

1 个答案:

答案 0 :(得分:1)

您不能只需要替换share

$this->app->singleton('redis', function ($app) {
    return new Database($app['config']['database.redis']);
});