在Laravel视图中列出所有已注册的变量 [英] List all registered variables inside a Laravel view
问题描述
我正在使用Laravel5.我想知道哪些是传递给视图本身内部的所有变量.
I am using Laravel 5. I would like to know which are all variables passed to a view inside the view itself.
因为所有变量都在视图范围内,所以我认为我可以使用通用PHP函数:get_defined_vars();
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
类似这样的东西:
// resources/view/home.blade.php
<html>
<body>
<?php print_r(get_defined_vars()); ?>
</body>
</html>
但是我想知道是否有更好的方法(例如View::getData()
)
But I would like to know if there is a better way (something like View::getData()
)
注意: get_defined_vars()无效,因为它返回了数百个无用的变量(Laravel组件)
Note: get_defined_vars() deosn't work becausee it returns hundreds of useless variables (Laravel components)
这是使用print_r(get_defined_vars())
的代码段(部分)(我认为它在无限递归循环中进行):
This is a snippet (partial) using print_r(get_defined_vars())
(i think it goes in infinite recursion loop):
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] =>
)
推荐答案
使用dd
帮助器:
{{ dd(get_defined_vars()) }}
了解更多: https://laravel.com/docs/5.4/helpers#method -dd
更新(thx,@ JoeCoder):您可以通过以下操作进一步削减无用"变量:
Update (thx, @JoeCoder): you can further cutdown on the "useless" variables by doing:
{{ dd(get_defined_vars()['__data']) }}
这篇关于在Laravel视图中列出所有已注册的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!