创建名为的新服务提供程序
ViewServiceProvider
,然后在其中,删除寄存器和引导方法,并使其扩展
Illuminate\View\ViewServiceProvider
.
然后,添加此方法:
public function registerBladeEngine($resolver)
{
// The Compiler engine requires an instance of the CompilerInterface, which in
// this case will be the Blade compiler, so we'll first create the compiler
// instance to pass into the engine so it can compile the views properly.
$this->app->singleton('blade.compiler', function () {
return new MyBladeCompiler(
$this->app['files'], $this->app['config']['view.compiled']
);
});
$resolver->register('blade', function () {
return new CompilerEngine($this->app['blade.compiler']);
});
}
注意在singleton方法中,我使用
你的
刀片编译器类。
然后,打开
config/app.php
,并替换
\Illuminate\View\BladeServiceProvider::class
,与您自己的服务提供商一起录制。
所以服务提供商应该是这样的:
namespace App\Providers;
use MyBladeCompiler
use Illuminate\View\ViewServiceProvider as BaseViewServiceProvider;
class ViewServiceProvider extends BaseViewServiceProvider
{
public function registerBladeEngine($resolver)
{
$this->app->singleton('blade.compiler', function () {
return new MyBladeCompiler(
$this->app['files'], $this->app['config']['view.compiled']
);
});
$resolver->register('blade', function () {
return new CompilerEngine($this->app['blade.compiler']);
});
}
}
这通过扩展照明视图服务提供程序来工作,因此所有现有的方法都按预期工作。然后,你需要覆盖
registerBladeEngine()
方法,以便调用重写的,而不是照明提供程序中的。
在重写的方法中,指定应使用编译器而不是原始编译器。
然后,通过编辑app.php配置文件指定使用扩展视图服务提供程序,而不是照明服务提供程序。