Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempting to use custom facade results in 'Call to undefined method' on Application::create() in Laravel?

I am attempting to create my own custom facade for a Search feature, but I'm having a little difficulty:

type: Symfony\Component\Debug\Exception\FatalErrorException
message: Call to undefined method Illuminate\Foundation\Application::create()
file: H:\myproj\vendor\laravel\framework\src\Illuminate\Container\Container.php
line: 165

This error is caused by my code hitting:

Search::indexObject(); 

Where my Search facade is set up as follows:

SearchServiceProvider

<?php
namespace MyProj\Search;

use Illuminate\Support\ServiceProvider;

class SearchServiceProvider extends ServiceProvider {

    public function register() {
        $this->app->bind('search', 'MyProj\Search\Search');
    }

}

Search Facade

<?php
namespace MyProj\Facades;

use Illuminate\Support\Facades\Facade;

class Search extends Facade {
    public static function getFacadeAccessor() {
        return 'search';
    }
}

Search class

<?php
namespace MyProj\Search;

use Elasticsearch\Client;
use Credential;

class Search {
    private $elasticSearchClient;

    public function __construct() {
        $this->elasticSearchClient = new Client(array(
            'hosts' => [Credential::ElasticSearchHost]
        ));
    }

    public function indexObject($object) {
        // Code 
        return $this->elasticSearchClient->index($params);
    }

    public function get() {
        return $this->$elasticSearchClient;
    }
}

I have run composer dump-autoload without success, and my facade and service provider is loaded in app.php as follows:

Aliases array

'Search'            => 'MyProj\Facades\Search',

Providers array

'MyProj\Search\SearchServiceProvider'

I've spent the past 30 minutes debugging and searching for this error without any fix. What's going on here?

EDIT: I've added in the stack trace, which you can see below. Additionally, I can see that getFacadeAccessor() is being called correctly, but anything beyond that is outside of my understanding.

enter image description here

The highlighted frame represents the last occurrence of normal operation, both frames on Handler.php represent the formatting and outputting of the error at the top of the question.

like image 634
marked-down Avatar asked Jul 26 '15 11:07

marked-down


1 Answers

Appreciate this is a bit of an old thread, however you'd get the problems described if you compiled cache wasn't cleared after adding your new Facade.

You should run:

php artisan clear-compiled

like image 69
Matt Avatar answered Nov 08 '22 18:11

Matt