Skip to content

Instantly share code, notes, and snippets.

@mewejo
Last active March 3, 2020 10:41
Show Gist options
  • Save mewejo/f43085c9ba7a4a6437db66c413847ad9 to your computer and use it in GitHub Desktop.
Save mewejo/f43085c9ba7a4a6437db66c413847ad9 to your computer and use it in GitHub Desktop.
Allow resolving services (like @ inject) with params in Blade
<?php
// Place inside boot() of your AppServiceProvider
Blade::directive('resolve', function($expression)
{
$segments = explode(',', $expression); // FYI The original has a preg_replace to remove ()"'
$variable = trim($segments[0], '\'" ');
$service = trim($segments[1], '\'" ');
$params = trim($segments[2], '\'" ');
if(!$params) $params = 'null';
return "<?php \${$variable} = app('{$service}', {$params}); ?>";
});
// Usage (like Blade's Inject directive, just with options for params)
@resolve('methodFormatter', 'App\Utilities\Stripe\StripeCardFormatter', [$method->asStripePaymentMethod()])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment