How to Install and Configure the PHP Redis Extension on Windows and Integrate It with Laravel
This guide walks through installing the PHP Redis extension on Windows, configuring the extension and Redis server, and integrating Redis with a Laravel application using the predis client, including code snippets for setup, configuration files, and testing cache functionality.
Redis is a popular open‑source key‑value store written in ANSI C, widely used in modern development. This tutorial explains how to install the PHP Redis extension on a Windows development environment, configure the extension, start the Redis server, and embed Redis into a Laravel project.
1. Install the PHP Redis extension (Windows)
Download the appropriate DLL files from the official Windows PECL repository: http://windows.php.net/downloads/pecl/snaps/redis/2.2.5/ Copy the downloaded .dll files into the .../php/ext directory and add the following lines to php.ini:
; php_redis extension=php_igbinary.dll
extension=php_redis.dllRestart Apache or Nginx and verify the installation by visiting http://localhost/phpinfo.php and searching for “redis”.
2. Install the Redis server
Download the Windows Redis binary package:
https://github.com/MSOpenTech/redis/releases/download/win-2.8.19.1/redis-2.8.19.zipExtract the archive, navigate to the folder, and start the server with: redis-server.exe redis.conf The server should now be running and visible in the task manager.
3. Add the Predis client to Laravel
In composer.json add the Predis package and run the update command:
"require": {
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"guzzlehttp/guzzle": "^6.3",
"huying/sms-ronglian": "^1.0@dev",
"predis/predis": "*"
}, composer updateThen modify the cache configuration ( config/cache.php) to use Redis: 'default' => env('CACHE_DRIVER', 'redis'), and update the Redis settings in config/database.php:
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],4. Test Redis usage in Laravel
Create a test route that checks for a cached value and creates it if missing:
Route::get('/index/test', function () {
if (Cache::has('laravel-redis')) {
// read cache
echo Cache::get('laravel-redis');
} else {
// create cache
$redis = Cache::add('laravel-redis', '我是缓存', 5);
echo Cache::get('laravel-redis');
}
});Access the route (e.g., http://localhost/blog/public/index.php/index/test) and verify the output.
You can also query Redis directly via the CLI: redis 127.0.0.1:6379> keys *1 "laravel:test" Following these steps completes the installation and basic integration of Redis with a Laravel application. Deeper usage patterns will be covered in future articles.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
