问题
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