Back

Laravel 5 Facade - Class not found if called from template.


Hi guys,

i'm trying to understand what wrong with my facade code. I create a new facade called Ajax in this way

namespace App\Facades\Ajax\Facade;

use Illuminate\Support\Facades\Facade;
/**
 * Description of Ajax Facade Accessor
 *
 * @author Administrator
 */
class Ajax extends Facade{
    //put your code here
    protected static function getFacadeAccessor() { return 'ajax'; }
}

namespace App\Facades\Ajax;

use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Config;
use App\Repositories\Currency\CurrencyInterface;
use App\Currency as CurrencyModel;
use App\Repositories\Language\LanguageInterface;
use Illuminate\Support\Facades\App;

/**
 * Description of Ajax Facade Class
 *
 * @author Administrator
 */
class Ajax {

    public function __construct() { }
   
    public function test(){ echo 'test!';}

}

I also created a FacadeServiceProvider that is registered into the app.php file in this way

'providers' => [
//............
'App\Providers\FacadeServiceProvider',
//............
]

and created an alias from it that is always registered into the app.php

'aliases'  => [
//........
 'Ajax' => 'App\Facades\Ajax\Facade\Ajax',
],

and bind the facade in the application

use Illuminate\Support\ServiceProvider;
use App\Facades\Ajax\Ajax;

class FacadeServiceProvider extends ServiceProvider {

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot() {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register() {

        // Ajax Facade
        $this->app->bind('ajax', function($app) {
            return new Ajax();
        });
        
    }

Now if I call this facade function from a controller in the way below it work perfectly


use App\Facades\Ajax\Facade\Ajax;


class TestController extends Controller{

 public function index(){
  Ajax::test();    //this work perfectly
 }
}

but if I call him from a blade template in this way.

<!-- index.blade.php -->
{{ Ajax::test() }}  is not working


{!! Ajax::test() !!} is not working 

The error I get is 'Class Ajax not found'

To help you better understand I've got another facade registered in the same way called Currency and it work perfectly both from the controller and from the blade template...

Any ideas?? Thank you all.

Xum replied 4 years ago

Facades are supposed to live in global namespace. Try to use \Ajax::test() instead of Ajax::test in your controller, and see if it works. If it doesn't, your alias is not registering properly.

luca83 replied 4 years ago

In my controller it work perfectly in both ways \Ajax::test() and simply Ajax::test() but is not working if I call it from my template file in this way {{ Ajax::test() }} or {!! Ajax::test() !!}

I'm also deleted the cache with php artisan cache command and also force to regenerate the compiled.php file that is inside the /vendor folder..but still nothing.

The alias is registered into the app.php file like any other Facade aliases.

any other ideas?

Xum replied 4 years ago

Right, you should also remove use App\Facades\Ajax\Facade\Ajax; from your controller.

Or, alternatively, try Ajax::test() in php artisan tinker console.

luca83 replied 4 years ago

Ok..done already this test using tinker with two different facade defined in the same way..below the result:

Psy Shell v0.4.4 (PHP 5.5.9-1ubuntu4.9 — cli) by Justin Hileman
>>> Currency::test()
hello Currency⏎
=> null
>>> Ajax::test()
PHP Fatal error:  Class 'Ajax' not found in eval()'d code on line 1

both are in app.php aliases and also registered into the FacadeServiceProvider that is loaded into the app.php as showed above.

full FacadeServiceProvider code

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Facades\Currency\Currency;
use App\Repositories\Currency\CurrencyEloquent;
use App\Currency as CurrencyModel;
use App\Facades\Language\Language;
use App\Language as LanguageModel;
use App\Repositories\Language\LanguageEloquent;
use App\Facades\Helper\Helper;
use App\Facades\Ajax\Ajax;

class FacadeServiceProvider extends ServiceProvider {

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot() {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register() {

        // Repository Facade
        $this->app->bind('currency', function($app) {
            return new Currency(new CurrencyEloquent(new CurrencyModel()), new LanguageEloquent(new LanguageModel()));
        });

        // Language Facade
        $this->app->bind('language', function($app) {
            return new Language();
        });

        // Helper Facade
        $this->app->bind('helper', function($app) {
            return new Helper(
                    $this->app->make('App\Repositories\Generic\GenericInterface'), 
                    $this->app->make('App\Repositories\Auction\AuctionInterface'), 
                    $this->app->make('App\Repositories\Lot\LotInterface'), 
                    $this->app->make('App\Repositories\Good\GoodInterface')
            );
        });

        // Ajax Facade
        $this->app->bind('ajax', function($app) {
            return new Ajax();
        });
        
    }

}

ideas?

Xum replied 4 years ago

Does the log file have a backtrace for the "Class not found" error?

luca83 replied 4 years ago

The test :

class AjaxTest extends TestCase{
    //put your code here
    
    public function tearDown() {
        parent::tearDown();
        Mockery::close();
    }
    
    public function testTest(){
        \Ajax::shouldReceive('test')->withAnyArgs();
//      \Currency:shouldReceive('test')->withAnyArgs();       // WORK FINE
        
    }
    
}

The trace :

PHPUnit 4.6.6 by Sebastian Bergmann and contributors.

Configuration read from /var/www/html/immogobid/phpunit.xml

.PHP Fatal error:  Class 'Ajax' not found in /var/www/html/immogobid/tests/AjaxTest.php on line 24
PHP Fatal error:  Uncaught exception 'Illuminate\Container\BindingResolutionException' with message 'Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable.' in /var/www/html/immogobid/vendor/laravel/framework/src/Illuminate/Container/Container.php:785
Stack trace:
#0 /var/www/html/immogobid/vendor/laravel/framework/src/Illuminate/Container/Container.php(656): Illuminate\Container\Container->build('Illuminate\Cont...', Array)
#1 /var/www/html/immogobid/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(644): Illuminate\Container\Container->make('Illuminate\Cont...', Array)
#2 /var/www/html/immogobid/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(152): Illuminate\Foundation\Application->make('Illuminate\Cont...')
#3 /var/www/html/immogobid/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(73): Illuminate\Foundation\Bootstrap\HandleExceptions->getExceptionHandler()
#4 /var/www/html/immogobid/vendor/laravel/framework/src/Illuminat in /var/www/html/immogobid/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 785

really don't know what to do..I check everything. I understand that for some reason this class is not instantiated by laravel but don't know why...Also look like any other facade I define are not working anymore..I'm gonna try to put the same code in a fresh project and see what happen! I'll let you know.

luca83 replied 4 years ago

Ok..same code on different project and it work perfectly! what can I do? I can't rewrite all the code because is a big project...any ideas? thank you all..

luca83 replied 4 years ago Solution

Ok..reload all my code from my machine to the server, reset all permission and the problem is fixed! Thank you Xum! Probably there was something missed or some file permissions.

Problem fixed.

davestewart replied 4 years ago

Bloody file permissions!

I'm moving from Windows to Mac at the moment and this is a constant issue.

I'm actually looking at going on some kind of network admin course so I can get my head round this once and for all


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us