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

狂风中的少年 提交于 2019-12-23 12:26:14

问题


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:


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!



来源:https://stackoverflow.com/questions/30735429/laravel-5-1-call-to-undefined-method-illuminate-view-compilers-bladecompiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!