Skip to content

Instantly share code, notes, and snippets.

@bslima
Created August 11, 2013 22:25
Show Gist options
  • Save bslima/6207163 to your computer and use it in GitHub Desktop.
Save bslima/6207163 to your computer and use it in GitHub Desktop.
How to enable laravel 3.x redis connection to take password.
... ... @@ -17,6 +17,13 @@ class Redis {
17 17 protected $port;
18 18
19 19 /**
20 + * The password for connect to Redis.
21 + *
22 + * @var string
23 + */
24 + protected $password;
25 +
26 + /**
20 27 * The database number the connection selects on load.
21 28 *
22 29 * @var int
... ... @@ -45,10 +52,11 @@ class Redis {
45 52 * @param int $database
46 53 * @return void
47 54 */
48 - public function __construct($host, $port, $database = 0)
55 + public function __construct($host, $port, $database = 0, $password = null)
49 56 {
50 57 $this->host = $host;
51 58 $this->port = $port;
59 + $this->password = $password;
52 60 $this->database = $database;
53 61 }
54 62
... ... @@ -79,7 +87,7 @@ public static function db($name = 'default')
79 87
80 88 extract($config);
81 89
82 - static::$databases[$name] = new static($host, $port, $database);
90 + static::$databases[$name] = new static($host, $port, $database, $password);
83 91 }
84 92
85 93 return static::$databases[$name];
... ... @@ -153,6 +161,11 @@ protected function connect()
153 161 throw new \Exception("Error making Redis connection: {$error} - {$message}");
154 162 }
155 163
164 + //If the password is set, then calls for auth command to allow authentication
165 + if ( ! is_null( $this->password ) ) {
166 + $this->auth( $this->password );
167 + }
168 +
156 169 $this->select($this->database);
157 170
158 171 return $this->connection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment