douyan6871 2017-04-18 12:05
浏览 203
已采纳

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

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;
    }
}
  • 写回答

1条回答 默认 最新

  • douyousu9691 2017-04-18 12:49
    关注

    You can't just replace share you need to do:

    $this->app->singleton('redis', function ($app) {
        return new Database($app['config']['database.redis']);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?