Laravel 5.1 - Call to undefined method Illuminate\View\Compilers\BladeCompiler::createPlainMatcher()

5.2k views Asked by At

Trying to upgrade my project from L5 to L5.1 and here is incompatibility:

Call to undefined method Illuminate\View\Compilers\BladeCompiler::createPlainMatcher()

Here's the code which cause an exception:

Blade::extend(function($view, $compiler) {
    $pattern = $compiler->createPlainMatcher('spaceless');
    return preg_replace($pattern, '$1<?php ob_start(); ?>$2', $view);
});

Blade::extend(function($view, $compiler) {
    $pattern = $compiler->createPlainMatcher('endspaceless');
    return preg_replace($pattern, '$1<?php echo trim(preg_replace(\'/>\s+</\', \'><\', ob_get_clean())); ?>$2', $view);
});

What should I change to make this code working in Laravel 5.1?

1

There are 1 answers

0
whoacowboy On

I had the same problem. I looked into @lukasgeiter comment and that does work, and I will use that going forward. He is referring to adding a blade directive call to AppServiceProvider.

public function boot()
{
    Blade::directive('datetime', function($expression) {
        return "<?php echo with{$expression}->format('m/d/Y H:i'); ?>";
    });
}

I had created a blade specific service provider for my Laravel 5.0 app and have a few custom functions that I didn't want to rewrite so I added the createOpenMatcher function my custom BladeServiceProvider.

In my case I added it like so.

<?php namespace App\Providers;

use Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Compilers\BladeCompiler;

class BladeServiceProvider extends ServiceProvider {

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::extend(function($view, $compiler) {
            $pattern = $this->createOpenMatcher('spaceless');
            return preg_replace($pattern, '$1<?php ob_start(); ?>$2', $view);
        });

        Blade::extend(function($view, $compiler) {
            $pattern = $this->createOpenMatcher('endspaceless');
            return preg_replace($pattern, '$1<?php echo trim(preg_replace(\'/>\s+</\', \'><\', ob_get_clean())); ?>$2', $view);
        });
    }

    public function createOpenMatcher($function){
        return '/(?<!\w)(\s*)@'.$function.'\(\s*(.*)\)/';
    }
}

Hope this helps!