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个回答



您不能只需要替换 share </ code>:</ p>

< pre> $ this-&gt; app-&gt; singleton('redis',function($ app){
返回新数据库($ app ['config'] ['database.redis']);
});
</ code> </ pre>
</ div>

展开原文

原文

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

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

doufeikuang7724
doufeikuang7724 我在命令行中得到这个[Symfony \ Component \ Debug \ Exception \ FatalErrorException]找不到类'Illuminate \ Redis \ Database'PHP致命错误:在'\ vendor \ vetruvet \中找不到类'Illuminate \ Redis \ Database' 第7行上的laravel-phpredis \ src \ Vetruvet \ PhpRedis \ Database.php脚本php artisan优化处理返回的post-update-cmd事件,错误代码为255
3 年多之前 回复
doucang6739
doucang6739 尝试删除您的vendor / laravel目录并重新运行composer install以防万一。
3 年多之前 回复
dongyukui8330
dongyukui8330 我已经添加了当前在不同文件中设置的phpredis的所有代码。 如果您发现有问题,请查看它并告诉我
3 年多之前 回复
dongshike7171
dongshike7171 不确定是否需要redis客户端(我认为不是)但你需要检查你的config / app.php以确保Redis的服务提供商在PhpRedis的服务提供商之前运行。 另外我不知道为什么它找不到Illuminate \ Redis \ Database。 它应该包含在laravel中。
3 年多之前 回复
doujian7132
doujian7132 我所做的只是将cache_driver从文件更改为.env中的redis,并在app.php中将Redis更改为LRedis并在composer.json文件中添加了一行代码“vetruvet / laravel-phpredis”:“1。*”这将安装phpredis 。 我的计算机中没有redis客户端或任何类似的东西。 我需要安装它才能工作吗? 是我缺少的东西,导致redis数据库错误?
3 年多之前 回复
douqiu0351
douqiu0351 确保您的Redis服务提供商在此之前运行。
3 年多之前 回复
dpjs2005
dpjs2005 我收到此错误:[Symfony \ Component \ Debug \ Exception \ FatalErrorException]找不到类'Illuminate \ Redis \ Database'
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐