B
B
BonBon Slick2017-04-25 16:31:21
Laravel
BonBon Slick, 2017-04-25 16:31:21

Class App\Elastic\Elastic does not exist when it clearly exists?

Laravel 4.2, in Illuminate\Support\ServiceProvider

public function boot() {
        $elastic = $this->app->make(\App\Elastic\Elastic::class);

        Post::saved(function ($post) use ($elastic) {
            $elastic->index([
                'index' => 'blog',
                'type' => 'post',
                'id' => $post->id,
                'body' => $post->toArray()
            ]);
        });
    }

The class is clearly there, PhPStorm goes and finds it, but according to the code, Laravel does not see the class.
namespace App\Elastic;
use Elasticsearch\Client;

class Elastic
{....

I have already created and bound my ServiceProvider, dug out in the docks:
<?php
namespace App\Elastic;
use Illuminate\Support\ServiceProvider;

class ElasticServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(Elastic::class, function ($app) {
            return new Elastic(
                ClientBuilder::create()
                    ->setLogger(ClientBuilder::defaultLogger(storage_path('logs/elastic.log')))
                    ->build()
            );
        });
    }

}

Now the error that the Elastic class is not found is announced in the controller:
public function searchUser()
    {
        $elastic = app(Elastic::class);

It’s as if namespace works somehow differently here than in standard PHP and Laravel 5.
After digging around on the forums, I found that in 4.2 namespace works implicitly, through well. And there are several options for solving the alleged problem. Well, I tried it like this:
"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/Elastic", 
       "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ]

I ran the command composer dumpautoloadand to no avail, the class is still not found when it is clearly there. Well, I threw the class back into the models folder iiii, now it gives an error that the class already exists .... omg.
It's very strange, even with the new Neuspace it only works like this:
public function searchUser()
    {
        $elastic = app()->make(App\Services\Elastic::class);
        dd($elastic);

Now it throws an error:
Illuminate \ Container \ BindingResolutionException
Unresolvable dependency resolving [Parameter #0 [ <required> $retries ]] in class Elasticsearch\Transport

Apparently an error in the provider when binding, but where?
<?php

namespace App\Providers;

use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Application as App;

class AppServiceProvider extends ServiceProvider
{
    protected $app;

    public function __construct(App $app)
    {
        $this->app = $app;
    }

    public function register()
    { // пробовал и Elastic::class / Client::class
        $this->app->bind(\App\Services\Elastic::class, function () {
            return \Elasticsearch\ClientBuilder::create()->build();
        });
    }
}

Why?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question