Created
January 4, 2020 10:33
-
-
Save calebporzio/03d610c813d6639e96accc3b06892242 to your computer and use it in GitHub Desktop.
Blade Route Directives
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 | |
// Register these inside a service provider: | |
Blade::directive('route', function ($expression) { | |
return "<?php echo route({$expression}) ?>"; | |
}); | |
Blade::directive('routeIs', function ($expression) { | |
return "<?php if (request()->routeIs({$expression})) : ?>"; | |
}); | |
Blade::directive('endrouteIs', function () { | |
return "<?php endif; ?>"; | |
}); |
a small fix to @markvaneijk solution...
For IF directives to work, it has to return boolean instead of a string
Blade::if('routeIs', function ($expression) {
return request()->routeIs($expression);
});
You’re right, I wrote it too quickly 😅
Copy pasted it, and wondered why it didn't work 😅
(felt like 2000's when I was doing it the same aka script kiddie style)
didn't know of the IF directive though, so thanks for pointing it out! 🤓
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use these things also often. Works very nice. The second one could be shorter as a custom Blade If: