Search code examples
packagelaravel-8lumen

Target class [blade.compiler] does not exist. in Lumen can we use balde in lumen provider


In Lumen can we use Blade in the Lumen provider?

Target class [blade.compiler] does not exist.

namespace App\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class RolesServiceProvider extends ServiceProvider
{
    /**
     * @return void
     */
    public function register()
    {
    }

    /**
     * @return void
     */
    public function boot()
    {
        Blade::directive('role', function ($role) {
            return "<?php if(auth()->check() && 
                auth()->user()->hasRole({$role})) :";
        });

        Blade::directive('endrole', function ($role) {
            return "<?php endif; ?>";
        });
    }
}

Solution

  • In your scenario, it's happening because I believe that you forgot to register the Illuminate\View\ViewServiceProvider class.

    Also, when registering the Provider, make sure to use $app->configure('view') in your bootstrap/app.php or $this->app->configure('view') from your Service Provider to configure your view configuration. Because the view service provider doesn't load the configuration itself.

    You can check how the view component is loaded in a Lumen application.

    I did like the following

    if (!$this->app->bound('view')) {
        // Lumen doesn't load the view config by default
        $this->app->configure('view');
        $this->app->register(ViewServiceProvider::class);
    }
    

    Or you can do the loadComponent thing as lumen does. And it will solve the issue you stated.