Configuring PhpRedis in Laravel 7 - laravel

I've set up a fresh installation of Laravel in Homestead and I've installed PhpRedis as recommended in the Laravel docs https://laravel.com/docs/7.x/redis#phpredis.
I followed this guide for installing PhpRedis https://webstoked.com/install-phpredis-laravel-ubuntu/
In both the Laravel docs, and the guide I've linked for installing PhpRedis, I'm instructed to rename the Redis alias in config/app.php.
If you plan to use PhpRedis extension along with the Redis Facade alias, you should rename it to something else, like RedisManager, to avoid a collision with the Redis class. You can do that in the aliases section of your app.php config file.
- Laravel Docs
To further add to my confusion, the Laravel docs then go on to say that you should remove the alias entirely.
To avoid class naming collisions with the Redis PHP extension itself, you will need to delete or rename the Illuminate\Support\Facades\Redis facade alias from your app configuration file's aliases array. Generally, you should remove this alias entirely and only reference the facade by its fully qualified class name while using the Redis PHP extension.
- Laravel Docs
My main questions are:
What does, "If you plan to use PhpRedis extension along with the Redis Facade alias", mean?
When should I rename the alias, remove it, or leave it as-is?
Depending on if I rename or remove the alias, how will this affect using Redis?

There are two different configurations/ways to use redis in a laravel project.
First one is to use predis and it is in your vendor folder. This one is "Flexible and feature-complete Redis client for PHP and HHVM" located here. It is a package/library written in php.
The other way to do is using PhpRedis, it is an extension written in C and located here.
protected function connector()
{
switch ($this->driver) {
case 'predis':
return new Connectors\PredisConnector;
case 'phpredis':
return new Connectors\PhpRedisConnector;
}
}
What does, "If you plan to use PhpRedis extension along with the Redis Facade alias", mean?
In the framework there is a check. While creating the PhpRedis client of Redis, it is checking whether the new Redis instance is Facade because PhpRedis is also using Redis name is you can see from here. So if you want to use PhpRedis in your laravel framework you better rename your facade because it will cause collision.
When should I rename the alias, remove it, or leave it as-is?
If you are going to use predis as client, then you can leave it as-is. If you are going to use PhpRedis as client, then you need to rename alias.
Depending on if I rename or remove the alias, how will this affect using Redis?
You will use RedisManager::someMethod() if you choose PhpRedis. You will use Redis::someMethod() if you use predis.

Related

Laravel configuration usage for different servers in pure php file

I am new in Laravel and want to know such problem
I am running an web application on several server using Laravel.
But I have encountered with an issue.
When there is modification for the project, I need to sync with git on several servers.
But it has different settings for each server (eg: DB name, DB password...)
I have set it manually because I couldn't use .env or configuration file since the file is just pure php file.
The issue I want to solve is how can I get Laravel configuration data from pure PHP file(not controller or whatever).
It will be thankful if someone teach me solution.
You asked:
how can I get Laravel configuration data from pure PHP file
The answer would be this:
You just make some file in config/ folder of laravel app (or use the existing file like config/app.php)
You make an array of your key value pairs
<?php
return [
'some_key' => 'some_value',
...
and you simply call it with this code where ever you need it:
config('config_file.key');
for example
config('app.name');
would give you Laravel by default.

"Non-static method Redis::get() cannot be called statically" in production but not in local

While developing in the local environment I haven't any problems using Redis in my master.blade.php
<body class="{{Redis::get('app_theme')}}">
untill I deployed my project (Forge+DigitalOcean) and started getting:
ErrorException: Non-static method Redis::get() cannot be called statically
My only thought was is might be deal with PHP itself, but versions are the same.
What else it might be??
UPD:
in my config/app.php specified that
'Redis' => Illuminate\Support\Facades\Redis::class,
You may not be using PHPRedis locally vs on your production.
As per Laravel documentation, if you are using the Redis facade along with PHPRedis, you should use a different name than "Redis" when specifying the alias in your app.php config file:
If you plan to use PhpRedis extension along with the Redis Facade
alias, you should rename it to something else, like RedisManager, to
avoid a collision with the Redis class. You can do that in the aliases
section of your app.php config file.
'RedisManager' => Illuminate\Support\Facades\Redis::class,
https://laravel.com/docs/7.x/redis#phpredis

How to use Laravel facades (Cache, Log, Storage) in package outside Laravel

Please point out any naivete or incorrect assumptions I'm making about Laravel, Composer, PHPUnit, etc.
I had a class called SpeechToTextHelper that was inside a Laravel project, and it used facades like this:
use Cache;
use Log;
use Storage;
Then, since I wanted to share it between multiple Laravel projects, I moved it into a separate repo and required it (into the first project) as a dependency via Composer.
The code all seems to run fine.
My question is different from Using Laravel Facades outside Laravel
What I want to know is:
Now that I also want to write PHPUnit tests for SpeechToTextHelper in my new tools repo, I see errors like RuntimeException: A facade root has not been set. and Error: Class 'Log' not found, presumably because this tools repo has no awareness of Laravel. I guess this means my production code has been working just by side-effect.
In my new tools repo (where my SpeechToTextHelper now is), how am I supposed to indicate (maybe somewhere in composer.json?) that the code will only work if Laravel's facades exist and are initiated properly?
How can I fix my separate repo's code so that its tests can run and also so that it ensures that it can only be "required" by a Laravel project?
P.S. https://laravel.com/docs/5.7/facades says "When building a third-party package that interacts with Laravel, it's better to inject Laravel contracts (https://laravel.com/docs/5.7/contracts) [which live in their own GitHub repository] instead of using facades." "If you are building a package, you should strongly consider using contracts since they will be easier to test in a package context."
But I do not see contracts for Log or Storage at all.
I think you are looking for Laravel component repositories
Cache - This component shows how to use Laravel's Cache features in non-Laravel applications.
Log - This component shows how to use Laravel's Log features in non-Laravel applications.
This video shows, how you can use eloquent outside laravel, I think that will give you better idea.
I'm not positive that this is the best approach, so I'd love if others
would provide better answers.
For production code
My composer.json still has this in the "require" section: "laravel/framework": "5.7.*",.
I plan to only ever require this tools library from within a Laravel app. I'm not sure that this is the right way to make that a rule, but my production code at least seems to be working.
For tests
As for tests, what seems to have been necessary was to add these files from https://github.com/laravel/laravel/tree/2a1f3761e89df690190e9f50a6b4ac5ebb8b35a3:
app/Console/Kernel.php
app/Providers/AppServiceProvider.php
app/Providers/AuthServiceProvider.php
app/Providers/EventServiceProvider.php
app/Providers/RouteServiceProvider.php
bootstrap/cache/.gitignore
bootstrap/app.php
bootstrap/autoload.php
config/app.php
config/database.php
config/logging.php
config/view.php
storage/logs/laravel.log
tests/CreatesApplication.php
tests/TestCase.php
Perhaps those are the minimum set of barebones Laravel files without which tests can't run.
Then I made sure that each test class extended tests/TestCase.php. And I adjusted the namespaces.

Lumen file cache driver

I'm in Lumen, inside a Controller, and I would like to cache a computation's result in a simple and easy way, without using database or external services, so I was looking for save the cache in the filesystem. In Laravel's documentation there is cited the file driver:
By default, Laravel is configured to use the file cache driver, which
stores the serialized, cached objects in the filesystem.
And I can see it, configured as Default Cache Store, inside config/cache.php.
In Lumen's documentation i can't see anything about the file driver and I find nothing like the file cache.php inside Lumen installation.
So my question is if I can use the file cache driver in Lumen (by setting CACHE_DRIVER=file) or if it is discouraged, not supported, not implemented or something else?
In Lumen in .env.example you have by default:
CACHE_DRIVER=memcached
So all you need is to change filename from .env.example to .env and set
CACHE_DRIVER=file
If you read Caching in Lumen you'll see in example:
$value = Cache::store('file')->get('foo');
so file driver is supported by Lumen.
If you also read Lumen Configuration you can read here that you can copy configuration files you need (in case you need them) and load them manually. You can see default Luman cache config file here: https://github.com/laravel/lumen-framework/blob/5.1/config/cache.php

Laravel selfregistering ServiceProvider

In Laravel 4.2 is it possible to create a package which automatically registers the ServiceProvider without the user adding the path manually to the app.php file?
This way one could just run composer update after adding a package to the composer.json file and would be ready to go.
I think that there is no way to register your main service provider than putting it in the app.php file. In fact you can, but it will always require something from the developer. You can't get away with it with a simple composer update.
You can register sub service providers with App::register('MyApp\Providers\MyServiceProvider'); inside your package.
No, there is no way for a package to register itself.

Resources