Guzzlehttp\Exception\ConnectionException: cURL error 28 resolving timeout after 2851 milisecond - php

I have recently integrated Algolia with my laravel application using Laravel-Scout library. Whenever, I try to search to search any products using algolia, I get GuzzelHttp\Exception\Connection\Exception. Following is the screen output of the response. The same issue also appears when I sync my database with Algolia's server. I have doubled checked my Algolia credentials in my project and they match correctly. I am running this project in linux mint-18.04 LTS, using default laravel server (neither Apache nor Nginx) and MySQL server.

It can be solved in the following way, given that you have already added Scout to your project:
Create your own app\Scout\EngineManager.php:
<?php declare(strict_types = 1);
namespace App\Scout;
use Algolia\AlgoliaSearch\Config\SearchConfig;
use Algolia\AlgoliaSearch\SearchClient as Algolia;
use Algolia\AlgoliaSearch\Support\UserAgent;
use Laravel\Scout\EngineManager as BaseEngineManager;
use Laravel\Scout\Engines\AlgoliaEngine;
class EngineManager extends BaseEngineManager
{
/**
* Create an Algolia engine instance.
*
* #return \Laravel\Scout\Engines\AlgoliaEngine
*/
public function createAlgoliaDriver()
{
$this->ensureAlgoliaClientIsInstalled();
UserAgent::addCustomUserAgent('Laravel Scout', '7.0.0');
$config = SearchConfig::create(config('scout.algolia.id'), config('scout.algolia.secret'));
$config->setConnectTimeout(10);
$algolia = Algolia::createWithConfig($config);
return new AlgoliaEngine(
$algolia,
config('scout.soft_delete')
);
}
}
Create your own app\Scout\Searchable.php:
<?php declare(strict_types = 1);
namespace App\Scout;
use Laravel\Scout\Searchable as BaseSearchable;
trait Searchable
{
use BaseSearchable;
/**
* Get the Scout engine for the model.
*
* #return mixed
*/
public function searchableUsing()
{
return app(EngineManager::class)->engine();
}
}
Create your own app\Providers\ScoutServiceProvider.php:
<?php declare(strict_types = 1);
namespace App\Providers;
use App\Scout\EngineManager;
use Laravel\Scout\ScoutServiceProvider as BaseScoutServiceProvider;
class ScoutServiceProvider extends BaseScoutServiceProvider
{
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
$this->app->singleton(EngineManager::class, function ($app) {
return new EngineManager($app);
});
parent::register();
}
}
Exclude the default scout package from the package discovery in your composer.json:
"extra": {
"laravel": {
"dont-discover": [
"laravel/scout"
]
}
},
Add your ScoutServiceProvider to the provider list in config/app.php.
Run composer dumpautoload.
Whenever adding the Searchable trait to a model, add your own trait to it instead of Scout's.
If you would like to make the timeout environment-dependent, it can be extracted to a config setting. Also please keep it in mind that the above is a bare minimum for getting the Searchable trait to work with a longer timeout, other classes may also need to be overridden for other features to work.

Algolia has default timeouts and this error pops when requests are over the default (I believe 2 seconds). You can check how long requests might be taking for you with: time host [ALGOLIA_ID].algolia.net. Unfortunately, Laravel Scout doesn't have the ability to override this and I've not found a good workaround.

Related

Laravel webwizo shortcodes package Shortcodes class not found

I recently started using laravel so i'm a beginner, and right now i'm working on a small project which requires me use shortcodes(like the ones in wordpress).
So i searched for a little bit and found this package:
https://packagist.org/packages/webwizo/laravel-shortcodes
I ran the installation and usage the way it's written but i get the error : Class 'App\Providers\Shortcode' not found in the provider I have to make using the laravel make:provider command as specified in the package instructions, below is my exact usage and install code.
added this to the providers array :
/*
* shortcodes providers
*/
Webwizo\Shortcodes\ShortcodesServiceProvider::class,
App\Providers\ShortcodesServiceProvider::class,
Added this to aliases:
'Shortcode' => Webwizo\Shortcodes\Facades\Shortcode::class,
this is the content of my ShortcodesServiceProvider in app/providers:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Shortcodes\JobsForm;
class ShortcodesServiceProvider extends ServiceProvider
{
/**
Bootstrap the application services.
*
#return void
*/
public function boot()
{
//
}
/**
Register the application services.
*
#return void
*/
public function register()
{
Shortcode::register('jobs', JobsForm::class);
}
}
I use laravel 5.4 so that might be an issue.
The thing is the class obviously exists, it gives the Shortcodes class not found error because I think it searches for it in the app/providers/ShortcodesServiceProvider file, and obviously it's not there it's in the vendor file.
Is there something I'm missing i've checked and double checked, I can't seem to get this thing to work.
It shoould work considering it has an alias defined right ?
I used it in the view like this:
return view('quarx-frontend::pages.' . $page->template)->with('page', $page)->withShortcodes();
Thanks for taking the time to read this any help would be much appreciated.
If you need any more info I'll be glad to supply it.
p.s. sorry for bad english ,not a native speaker :P
It searches for Shortcode in the App\Providers; namespace and not in the root namespace where the Facade is defined.
You can fix this in App\Providers\ShortcodesServiceProvider.php by either doing:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Shortcodes\JobsForm;
use Shortcode;
class ShortcodesServiceProvider extends ServiceProvider
{
Or use \Shortcode
/**
* Register the application services.
*
* #return void
*/
public function register()
{
\Shortcode::register('jobs', JobsForm::class);
}
I would recommend the first option.

How to update composer.json from Laravel controller

So I'm building a Laravel package and there is a special features that requires updating composer by adding a psr-4 namespace which points to a directory in Laravel base path.
I have tried this so far but doesn't work.
$loader = include(base_path('vendor/autoload.php'));
$loader->add('Classes\Weather', base_path('modules'));
Later:
$weather = new Classes\Weather\WeatherSite();
You might check this thread, which gives some solutions:
https://github.com/composer/composer/issues/1906#issuecomment-51632453
After several trials and going through Composer documentation, I was able to come up with this which works:
NB: The reason why I needed this solution is to enable me add a Psr4 path automatically from a Laravel package without manually adding a specific path required in the package manually in composer.json
Add this in the boot method of your package service provider , mine is DigitlimitModuleServiceProvider
use Illuminate\Support\ServiceProvider;
class DigitlimitModuleServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$autoload = require base_path('vendor/autoload.php');
$autoload->addPsr4('Digitlimit\\Module\\', base_path('modules'));
$autoload->register();
if(!file_exists(base_path('modules'))){
\File::makeDirectory(base_path('modules'));
//works as long as there is permission
}
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
}
}

Laravel 5 dynamically run migrations

so I have created my own blog package in a structure of Packages/Sitemanager/Blog I have a service provider that looks like the following:
namespace Sitemanager\Blog;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
class BlogServiceProvider extends LaravelServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* #return void
*/
public function boot() {
$this->handleConfigs();
$this->handleMigrations();
$this->handleViews();
$this->handleRoutes();
}
/**
* Register the service provider.
*
* #return void
*/
public function register() {
// Bind any implementations.
$this->app->make('Sitemanager\Blog\Controllers\BlogController');
}
/**
* Get the services provided by the provider.
*
* #return array
*/
public function provides() {
return [];
}
private function handleConfigs() {
$configPath = __DIR__ . '/config/blog.php';
$this->publishes([$configPath => config_path('blog.php')]);
$this->mergeConfigFrom($configPath, 'blog');
}
private function handleTranslations() {
$this->loadTranslationsFrom(__DIR__.'/lang', 'blog');
}
private function handleViews() {
$this->loadViewsFrom(__DIR__.'/views', 'blog');
$this->publishes([__DIR__.'/views' => base_path('resources/views/vendor/blog')]);
}
private function handleMigrations() {
$this->publishes([__DIR__ . '/migrations' => base_path('database/migrations')]);
}
private function handleRoutes() {
include __DIR__.'/routes.php';
}
}
Now, what i would like to do is run the migrations dynamically if they have never been run before or within an installation process i suppose. I've seen in older documentation you could so something like this:
Artisan::call('migrate', array('--path' => 'app/migrations'));
However, this is invalid in laravel 5, how can I approach this?
Artisan::call('migrate', array('--path' => 'app/migrations'));
will work in Laravel 5, but you'll likely need to make a couple tweaks.
First, you need a use Artisan; line at the top of your file (where use Illuminate\Support\ServiceProvider... is), because of Laravel 5's namespacing. (You can alternatively do \Artisan::call - the \ is important).
You likely also need to do this:
Artisan::call('migrate', array('--path' => 'app/migrations', '--force' => true));
The --force is necessary because Laravel will, by default, prompt you for a yes/no in production, as it's a potentially destructive command. Without --force, your code will just sit there spinning its wheels (Laravel's waiting for a response from the CLI, but you're not in the CLI).
I'd encourage you to do this stuff somewhere other than the boot method of a service provider. These can be heavy calls (relying on both filesystem and database calls you don't want to make on every pageview). Consider an explicit installation console command or route instead.
After publishing the package:
php artisan vendor:publish --provider="Packages\Namespace\ServiceProvider"
You can execute the migration using:
php artisan migrate
Laravel automatically keeps track of which migrations have been executed and runs new ones accordingly.
If you want to execute the migration from outside of the CLI, for example in a route, you can do so using the Artisan facade:
Artisan::call('migrate')
You can pass optional parameters such as force and path as an array to the second argument in Artisan::call.
Further reading:
https://laravel.com/docs/5.1/artisan
https://laravel.com/docs/5.2/migrations#running-migrations
For the Laravel 7(and probably 6):
use Illuminate\Support\Facades\Artisan;
Artisan::call('migrate');
will greatly work.

Override Singleton in Laravel Container

I'm wondering if there's a simple way to override a singleton service set in the core of the Laravel framework?
e.g. I'm trying to rewrite the app:name command service '' with the following provider:
use Hexavel\Console\AppNameCommand;
use Illuminate\Console\Events\ArtisanStarting;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\ServiceProvider;
class NameCommandProvider extends ServiceProvider
{
/**
* Register any other events for your application.
*
* #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
public function boot(Dispatcher $events)
{
$events->listen(ArtisanStarting::class, function ($event) {
$event->artisan->resolve('command.app.name');
}, -1);
}
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
$this->app->singleton('command.app.name', function ($app) {
return new AppNameCommand($app['composer'], $app['files']);
});
}
}
I'm 100% everything is working due to extensive checks put no matter what order I put my service provider (above or below ConsoleSupportServiceProvider) it still loads the original AppNameCommand over my custom one.
I've already got a work around BUT it would be nice to know about the behaviour of singleton services for the future if this is at all possible? (This is using Laravel 5.2 if that makes any difference.)
There's actually a cleaner way to do this. You basically want to extend a core binding, which can be achieved by using the extend method:
$this->app->extend('command.app.name', function ($command, $app) {
return new AppNameCommand($app['composer'], $app['files']);
});
Jason Lewis has a really nice article regarding Laravel's IoC on Tutsplus. Make sure to check it out ;)
I looked at this case and it seems it not the easy one. If you use singleton in your custom Provider it will be finally overridden by default provider (deferred one) so it seems it won't be the way.
After checking that simple approach doesn't work, what you need to do in such case is analysing what is happening when Laravel registers this command.
So in your case you search first for command.app.name - you see it's in Illuminate\Foundation\Providers\ArtisanServiceProvider and there is method registerAppNameCommand you would like to probably override.
So now you look for occurences of ArtisanServiceProvider to see where it's launched - you see it's in Illuminate\Foundation\Providers\ConsoleSupportServiceProvider in $providers property (which you would like probably to change).
So finally you should look for occurrences of ConsoleSupportServiceProvider and you see it's in config/app.php.
So what you need to do in this case:
Change in config/app.php - change Illuminate\Foundation\Providers\ConsoleSupportServiceProvider into your custom one ConsoleSupportServiceProvider
In your custom one you should extend from \Illuminate\Foundation\Providers\ConsoleSupportServiceProvider but change in $providers from Illuminate\Foundation\Providers\ArtisanServiceProvider into your custom ArtisanServiceProvider
finally create custom ArtisanServiceProvider which will extend from \Illuminate\Foundation\Providers\ArtisanServiceProvider where you override registerAppNameCommand using custom class in singleton
Using this way you will achieve your goal (I've verified it that custom class will be used running command php artisan app:name).
Alternatively you might want in your custom ArtisanServiceProvider remove 'AppName' => 'command.app.name', from $devCommands and use your custom service provider as you showed where you register your singleton but I haven't tried this approach.

laravel 5 configuration service providers for local only

I just read the Laravel 5 doc on configuration based on your environment http://laravel.com/docs/5.0/configuration#environment-configuration
I understand how the .env files can be used to configure some variables but how do I configure what service providers get loaded based on the environment?
For example in my ''local'' environment I use this debugbar plugin which needs a service provider but I don't want to define it in config/app.php for production.
In Laravel 4.2 I could just create config/local/app.php but that doesn't seem to work anymore, what is the alternative?
This article was helpful for me:
https://mattstauffer.co/blog/conditionally-loading-service-providers-in-laravel-5
And this is what I've done and it works:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
if ($this->app->environment('local')) {
$this->app->register('Barryvdh\Debugbar\ServiceProvider');
}
}
}
I believe if you set APP_DEBUG to false in your .env file the debugbar will go away automatically.
Also I wrote a middleware class to take care of that.
The handle method could look like this in your case:
public function handle($request, Closure $next)
{
if (app()->environment('production'))
\Debugbar::disable();
else
\Debugbar::enable();
return $next($request);
}
Then you just add it to the list of middleware in app/Http/kernel.php and you're good to go.
I have some additional conditions and more environments so this exact code is not tested but it should work and you should get the idea.
Take care.

Categories