diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php
index e160df6..c74d822 100755
--- a/src/HtmlServiceProvider.php
+++ b/src/HtmlServiceProvider.php
@@ -5,6 +5,7 @@ namespace Collective\Html;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
+use Illuminate\View\Compilers\BladeCompiler;
class HtmlServiceProvider extends ServiceProvider
{
@@ -37,8 +38,7 @@ class HtmlServiceProvider extends ServiceProvider
$this->app->alias('html', HtmlBuilder::class);
$this->app->alias('form', FormBuilder::class);
- $this->setBladeDirectives('Html', get_class_methods(HtmlBuilder::class));
- $this->setBladeDirectives('Form', get_class_methods(FormBuilder::class));
+ $this->registerBladeExtensions();
}
/**
@@ -68,25 +68,31 @@ class HtmlServiceProvider extends ServiceProvider
}
/**
- * Set blade directives
- *
- * @param string $namespace
- * @param array $methods
+ * Register Blade directives.
*
* @return void
*/
- protected function setBladeDirectives($namespace, $methods)
+ protected function registerBladeExtensions()
{
- foreach ($methods as $method) {
- if (in_array($method, $this->directives)) {
- $snakeMethod = Str::snake($method);
- $directive = strtolower($namespace).'_'.$snakeMethod;
+ $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
+ $namespaces = [
+ 'Html' => get_class_methods(HtmlBuilder::class),
+ 'Form' => get_class_methods(FormBuilder::class),
+ ];
- Blade::directive($directive, function ($expression) use ($namespace, $method) {
- return "";
- });
+ foreach ($namespaces as $namespace => $methods) {
+ foreach ($methods as $method) {
+ if (in_array($method, $this->directives)) {
+ $snakeMethod = Str::snake($method);
+ $directive = strtolower($namespace).'_'.$snakeMethod;
+
+ $bladeCompiler->directive($directive, function ($expression) use ($namespace, $method) {
+ return "";
+ });
+ }
+ }
}
- }
+ });
}
/**