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

1

6

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.

Bounteous answered 26/7, 2015 at 11:1 Comment(4)
What version of Laravel are you using? Could you paste a few more lines of the stack trace?Boleyn
I guess you're using Laravel 4.2. Did you do any changes to the Laravel code? In line 163 I can see "$method = ($abstract == $concrete) ? 'build' : 'make';", so I see no way how "create()" could be called.Boleyn
Did you change the code? Could you paste some more lines of the stack trace?Boleyn
Nope, not as far as I'm aware. I've added in the stack trace so you can see what's going on. Let me know if I can provide more info.Bounteous
G
3

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

Giuseppe answered 10/6, 2016 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.