Last active
March 3, 2020 10:41
-
-
Save mewejo/f43085c9ba7a4a6437db66c413847ad9 to your computer and use it in GitHub Desktop.
Allow resolving services (like @ inject) with params in Blade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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