LaravelCollective / html

HTML and Form Builders for the Laravel Framework
MIT License
4k stars 812 forks source link

[L5.6] Blade directives not working #503

Open misog opened 6 years ago

misog commented 6 years ago

@form_text('name') is just not processed and is placed as text "@form_text('name')" in view files

I think the problem is with $this->app->afterResolving() in HtmlServiceProvider.php:

    /**
     * Register Blade directives.
     *
     * @return void
     */
    protected function registerBladeDirectives()
    {

        // <--- here it runs --->

        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {

            // <--- here it does not run --->

            $namespaces = [
                'Html' => get_class_methods(HtmlBuilder::class),
                'Form' => get_class_methods(FormBuilder::class),
            ];

            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 "<?php echo $namespace::$method($expression); ?>";
                        });
                    }
                }
            }
        });
    }
tshafer commented 6 years ago

What's your file name?

On Mar 17, 2018, at 5:49 PM, misog notifications@github.com wrote:

@form_text('name') is just processed as string "@form_text('name')" in view file

I think the problem is with $this->app->afterResolving() in HtmlServiceProvider.php:

/**
 * Register Blade directives.
 *
 * @return void
 */
protected function registerBladeDirectives()
{

    // <--- here it runs --->

    $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {

        // <--- here it does not run --->

        $namespaces = [
            'Html' => get_class_methods(HtmlBuilder::class),
            'Form' => get_class_methods(FormBuilder::class),
        ];

        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 "<?php echo $namespace::$method($expression); ?>";
                    });
                }
            }
        }
    });
}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

misog commented 6 years ago

File name of my view file? create.blade.php As a workaround I copied the anonymous function with some changes from above and inserted it into App\Providers\AppServiceProvider.php and now form blade directives work:

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $namespaces = [
            'Html' => get_class_methods(\Collective\Html\HtmlBuilder::class),
            'Form' => get_class_methods(\Collective\Html\FormBuilder::class),
        ];

        foreach ($namespaces as $namespace => $methods) {
            foreach ($methods as $method) {
                if (in_array($method, $this->form_directives)) {
                    $snakeMethod = \Illuminate\Support\Str::snake($method);
                    $directive = strtolower($namespace).'_'.$snakeMethod;

                    Blade::directive($directive, function ($expression) use ($namespace, $method) {
                        return "<?php echo $namespace::$method($expression); ?>";
                    });
                }
            }
        }
    }
tshafer commented 6 years ago

Hmm, I haven't heard of this problem. Does it not work with a fresh install of both laravel and the package?

On Mar 17, 2018, at 6:35 PM, misog notifications@github.com wrote:

Filename of my view file? create.blade.php

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

misog commented 6 years ago

Indeed, directives do not work on fresh L5.6 with just LaravelCollective/html installed. Steps to reproduce with PHP 7.2 on Windows:

  1. laravel new testhtml
  2. cd testhtml
  3. composer require "laravelcollective/html":"^5.4.0"
  4. Add Collective\Html\HtmlServiceProvider::class, to config/app.php
  5. Add 'Form' => Collective\Html\FormFacade::class, and 'Html' => Collective\Html\HtmlFacade::class, to config/app.php
{!! Form::text('first') !!} // works
@form_text('second') // does not work, shown as text
tshafer commented 6 years ago

@form_text('second') why the @ in front of it?