Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe you have two options:</p> <ol> <li>Overwrite the service provider with your own.</li> <li>Take advantage of the BladeCompiler's <code>extend()</code> method.</li> </ol> <h3>Option 1: Service provider copy &amp; paste</h3> <p>The key thing you want to do is overwrite the Blade compiler with your extended version. Because of that whole inheritance thing, you have to define it in pretty much the exact same way as the default service provider; particularly this chunk of code:</p> <pre><code>$resolver-&gt;register('blade', function() use ($app) { $cache = $app['path.storage'].'/views'; // This is where your extended class would go. $compiler = new BladeCompiler($app['files'], $cache); return new CompilerEngine($compiler, $app['files']); }); </code></pre> <p>You can access the View class' EngineResolver in a service provider via:</p> <pre><code>$this-&gt;app['view']-&gt;getEngineResolver() </code></pre> <p>All put together, you're left with something along these lines (replacing class names and modifying <code>use</code> where appropriate):</p> <pre><code>&lt;?php use Illuminate\Support\ServiceProvider; use Illuminate\View\Engines\CompilerEngine; class ExtendBladeServiceProvider extends ServiceProvider { public function register() { $resolver = $this-&gt;app['view']-&gt;getEngineResolver(); $resolver-&gt;register('blade', function() use ($app) { $cache = $app['path.storage'].'/views'; $compiler = new MyBladeCompiler($app['files'], $cache); return new CompilerEngine($compiler, $app['files']); }); } } </code></pre> <p>So yes, you are basically duplicating the code from the existing service provider. But you pretty much have to, to the best of my knowledge. I'm not sure why you're worried about changes to Laravel braking your application; you're literally depending on Laravel components, so potentially any changes will break things. That's a risk you take using components that you have no control over.</p> <h3>Option 2: BladeCompiler's <code>extend()</code> method.</h3> <p>This you can do pretty much anywhere it makes sense in your application. You won't be extending the <code>BladeCompiler</code> class in a traditional sense, but you can implement your own set of compile functions to run.</p> <p><code>extend()</code> adds the provided closure to an array that is iterated upon during compiling. It is essentially the same as extended class methods, and does not require you to re-instantiate things like the CompileEngine.</p> <p>One option to register these extensions is to use a service provider's <code>boot()</code> method (you can use <code>register()</code> too, just make sure you do it in the right order):</p> <pre><code>public function boot() { $blade = $this-&gt;app['view']-&gt;getEngineResolver()-&gt;resolve('blade')-&gt;getCompiler(); $blade-&gt;extend(function($value) use ($blade) { $matcher = $blade-&gt;createMatcher('myfeature'); return preg_replace($matcher, '&lt;?php echo MyFeature::make$2; ?&gt;', $value); }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload