canfone's avatar

Lumen 7 Class blade.compiler does not exist when building custom blade directives

I upgraded to Lumen 7.x and I can't figure out what did I miss. It returns Class blade.compiler does not exist . It seems some aliases are not properly loaded. Anyone here have encountered the same issue?

Here is my code.

bootstrap/app.php

$app = new Laravel\Lumen\Application(
    dirname(__DIR__)
);

$app->withFacades();
$app->withEloquent();

$app->register(App\Providers\AppServiceProvider::class);

app/Providers/AppServiceProvider.php

/**
 * Register any application services.
 *
 * @return void
 */
public function boot()
{
    $timezone = 'UTC';

    Blade::directive(
        'date',
        function ($expression) use ($timezone)
        {
            return "<?php echo ($expression)->timezone('$timezone')->format('Y-m-d'); ?>";
        }
    );
}

Every time I run php artisan command, it throws the error blade.compiler does not exists.

0 likes
2 replies
artcore's avatar

I used this in Lumen 6 maybe works in 7 too

if ($this->app instanceof \Laravel\Lumen\Application)
    {
      /**
       * @var BladeCompiler $blade
       */
      $blade = $this->app['blade.compiler'];
      
      $blade->directive('csrf', function ()
      {
        return '<?php echo csrf_field(); ?>';
      });
      
      $blade->directive('method', function ($method)
      {
        return <<<HTML
<?php echo '<input type="hidden" name="_method" value="'.$method.'">'; ?>
HTML;
      });
    }
1 like
canfone's avatar

@artcore Thanks! I'll give it a try. Your solution might work and i'll stop using facades. But I am just worried some facades like Blade are not working and I need to convert everything.

Please or to participate in this conversation.