php 列出 Laravel 视图中的所有注册变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29146879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List all registered variables inside a Laravel view
提问by giò
I am using Laravel 5. I would like to know which are all variables passed to a view inside the view itself.
我正在使用 Laravel 5。我想知道哪些是传递给视图本身内部视图的所有变量。
Since all variables are in the view scope I thought I could use the generic PHP function: get_defined_vars();
http://php.net/manual/en/function.get-defined-vars.php
由于所有变量都在视图范围内,我认为我可以使用通用的 PHP 函数:http: get_defined_vars();
//php.net/manual/en/function.get-defined-vars.php
Something like this:
像这样的东西:
// resources/view/home.blade.php
<html>
<body>
<?php print_r(get_defined_vars()); ?>
</body>
</html>
But I would like to know if there is a better way (something like View::getData()
)
但我想知道是否有更好的方法(比如View::getData()
)
Note:get_defined_vars() deosn't work becausee it returns hundreds of useless variables (Laravel components)
注意:get_defined_vars() 不起作用,因为它返回数百个无用的变量(Laravel 组件)
This is a snippet (partial) using print_r(get_defined_vars())
(i think it goes in infinite recursion loop):
这是一个片段(部分)使用print_r(get_defined_vars())
(我认为它进入无限递归循环):
Array
(
[__path] => C:\net\laravel\storage\framework\views/8e030a77b0bdbacc2c4182fc04420d1d
[__data] => Array
(
[__env] => Illuminate\View\Factory Object
(
[engines:protected] => Illuminate\View\Engines\EngineResolver Object
(
[resolvers:protected] => Array
(
[php] => Closure Object
(
[this] => Illuminate\View\ViewServiceProvider Object
(
[app:protected] => Illuminate\Foundation\Application Object
(
[basePath:protected] => C:\net\laravel
[hasBeenBootstrapped:protected] => 1
[booted:protected] => 1
[bootingCallbacks:protected] => Array
(
[0] => Closure Object
(
[static] => Array
(
[instance] => Illuminate\Bus\BusServiceProvider Object
(
[defer:protected] => 1
[app:protected] => Illuminate\Foundation\Application Object
*RECURSION*
)
)
[this] => Illuminate\Foundation\Application Object
*RECURSION*
)
[1] => Closure Object
(
[static] => Array
(
[instance] => Illuminate\Translation\TranslationServiceProvider Object
(
[defer:protected] => 1
[app:protected] => Illuminate\Foundation\Application Object
*RECURSION*
)
)
[this] => Illuminate\Foundation\Application Object
*RECURSION*
)
)
[bootedCallbacks:protected] => Array
(
)
[terminatingCallbacks:protected] => Array
(
)
[serviceProviders:protected] => Array
(
[0] => Illuminate\Events\EventServiceProvider Object
(
[app:protected] => Illuminate\Foundation\Application Object
*RECURSION*
[defer:protected] =>
)
回答by Limon Monte
Use the dd
helper:
使用dd
助手:
{{ dd(get_defined_vars()) }}
Read more: https://laravel.com/docs/5.4/helpers#method-dd
阅读更多:https: //laravel.com/docs/5.4/helpers#method-dd
Update (thx, @JoeCoder): you can further cutdown on the "useless" variables by doing:
更新(感谢,@JoeCoder):您可以通过执行以下操作来进一步减少“无用”变量:
{{ dd(get_defined_vars()['__data']) }}
回答by cheelahim
If you are using Laravel 5.1 which now allows to extend Blade with custom directives you might find this useful. You need to register directives in AppServiceProvider like in this exampleor create you own provider.
如果您使用的是 Laravel 5.1,它现在允许使用自定义指令扩展 Blade,您可能会发现这很有用。您需要像本示例一样在 AppServiceProvider 中注册指令或创建您自己的提供程序。
/**
* 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']"
);
});
回答by Leon
Kind of the same, but a bit tidier :
有点相同,但更整洁:
{{ dd($__data) }}
{{ dd($__data) }}
回答by TommyZG
For better readability and debugging purposes, you can also create a helper which turns output into an array.
为了更好的可读性和调试目的,您还可以创建一个将输出转换为数组的助手。
// as per comment from Braunson add this to custom helpers function in app\helpers.php and include it via composer.
if (! function_exists('da')) {
/**
* Dump the passed variables to array and end the script.
*
* @param mixed
* @return void
*/
function da()
{
array_map(function ($x) {
dd($x->toArray());
}, func_get_args());
}
}