未找到“Redis”类 laravel 8,使用 phpredis

Aay*_*hal 21 redis laravel phpredis

我正在使用 laravel 8,当我尝试运行时,它会抛出错误:

\n

php artsian config:clear

\n

在 laravel 上,这是我的配置值(因为我正在尝试使用 phpredis):

\n
'client' => env('REDIS_CLIENT', 'phpredis'),\n
Run Code Online (Sandbox Code Playgroud)\n

我现在可以使用 redis-cli 和 ping。不,只是我可以成功到达以下端点。

\n
    public function testRedis()\n    {\n        Redis::set('ping','pong');\n        $ping = Redis::get('ping');\n        dd($ping);\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

就打印pong成功了。

\n

但我收到的课程Redis未找到。每当我尝试奔跑时php artisan config:clear

\n

完整错误如下所示:

\n
  Class "Redis" not found\n\n  at vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:75\n     71\xe2\x96\x95      * @throws \\LogicException\n     72\xe2\x96\x95      */\n     73\xe2\x96\x95     protected function createClient(array $config)\n     74\xe2\x96\x95     {\n  \xe2\x9e\x9c  75\xe2\x96\x95         return tap(new Redis, function ($client) use ($config) {\n     76\xe2\x96\x95             if ($client instanceof RedisFacade) {\n     77\xe2\x96\x95                 throw new LogicException(\n     78\xe2\x96\x95                     extension_loaded('redis')\n     79\xe2\x96\x95                         ? 'Please remove or rename the Redis facade alias in your "app" configuration file in order to avoid collision with the PHP Redis extension.'\n\n      +10 vendor frames \n  11  app/Models/Setting.php:34\n      Illuminate\\Support\\Facades\\Facade::__callStatic()\n\n  12  app/Providers/AppServiceProvider.php:37\n      App\\Models\\Setting::getCachedValue()\n
Run Code Online (Sandbox Code Playgroud)\n

我的App\\Models\\Setting样子:

\n
<?php\n\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Support\\Facades\\Cache;\n\nclass Setting extends Model\n{\n    use HasFactory;\n    \n    protected $table = 'settings';\n    protected $guarded = ['id'];\n    protected $dates = ['created_at','updated_at'];\n\n    /**\n     * The attributes that should be cast to native types.\n     *\n     * @var array\n     */\n    protected $casts = [\n        'created_at' => 'datetime:Y-m-d', // Change your format\n        'updated_at' => 'datetime:Y-m-d',\n    ];\n\n    const STRIPE_ENVIRONMENT = ['test', 'live'];\n\n    public static function getCachedValue(){\n        return Cache::rememberForever('settings', function () {\n            return Setting::pluck('key_value', 'key_name');\n        });\n    }\n\n    public static function updateCachedSettingsData(){\n        Cache::forget('settings');\n        self::getCachedValue();\n    }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

什么,我可能在这里做错了。\n #PS:我的这一行config/app被注释了。

\n
// 'Redis' => Illuminate\\Support\\Facades\\Redis::class,\n
Run Code Online (Sandbox Code Playgroud)\n

小智 42

安装后必须在 .env 文件中指定“REDIS CLIENT”

composer require predis/predis
Run Code Online (Sandbox Code Playgroud)

在 .env 文件中添加这些行

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_CLIENT=predis
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,缺少“REDIS_CLIENT=predis” (4认同)

Aay*_*hal 15

好的,我发现了这个问题,有更新推送到ubuntu,这次更新看起来默认php已更新为php8.0,之前是7.4。因此,运行以下命令可以解决该问题。

sudo apt-get install php8.0-redis
Run Code Online (Sandbox Code Playgroud)

看起来它缺少 php 8.0 的 redis 扩展