Created
August 20, 2016 00:33
-
-
Save s00d/a94e0e06b21d58d3c4d033bc321b956b to your computer and use it in GitHub Desktop.
Blade directives to dump template variables.
This file contains hidden or 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 | |
namespace App\Providers; | |
use Illuminate\Support\Facades\Blade; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
/** | |
* Blade directive to dump template variables. Accepts single parameter | |
* but also could be invoked without parameters to dump all defined variables. | |
* It does not stop script execution. | |
* @example @d | |
* @example @d(auth()->user()) | |
*/ | |
Blade::directive('d', function ($data) { | |
return sprintf("<?php (new Illuminate\Support\Debug\Dumper)->dump(%s); ?>", | |
null !== $data ? $data : "get_defined_vars()['__data']" | |
); | |
}); | |
/** | |
* Blade directive to dump template variables. Accepts single parameter | |
* but also could be invoked without parameters to dump all defined variables. | |
* It works similar to dd() function and does stop script execution. | |
* @example @dd | |
* @example @dd(auth()->user()) | |
*/ | |
Blade::directive('dd', function ($data) { | |
return sprintf("<?php (new Illuminate\Support\Debug\Dumper)->dump(%s); exit; ?>", | |
null !== $data ? $data : "get_defined_vars()['__data']" | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment