列出Laravel视图中的所有已注册变量

giò*_*giò 52 php laravel laravel-5

我正在使用Laravel 5.我想知道哪些变量都传递给视图内部的视图.

由于所有变量都在视图范围内,我认为我可以使用通用的PHP函数:http:get_defined_vars(); //php.net/manual/en/function.get-defined-vars.php

像这样的东西:

  // resources/view/home.blade.php
  <html>
  <body>
       <?php print_r(get_defined_vars()); ?>
  </body>
  </html>
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有更好的方法(类似的东西View::getData())

注意: get_defined_vars()无效,因为它返回数百个无用的变量(Laravel组件)

这是一个片段(部分)使用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] => 
                                                                        )
Run Code Online (Sandbox Code Playgroud)

Lim*_*nte 81

使用dd帮手:

{{ dd(get_defined_vars()) }}
Run Code Online (Sandbox Code Playgroud)

阅读更多:https://laravel.com/docs/5.4/helpers#method-dd

更新(thx,@ JoeCoder):您可以通过以下方式进一步减少"无用"变量:

{{ dd(get_defined_vars()['__data']) }}
Run Code Online (Sandbox Code Playgroud)

  • 你可以通过`dd(get_defined_vars()['__ data'])进一步减少"无用的"变量. (4认同)
  • @llnk如果按照建议使用`dd`,那些"无用"变量将被折叠,最终只显示少数(顶级变量).查看数据的方式更简单(与`print_r`相对). (2认同)

Leo*_*eon 6

有点相同,但更整洁:

{{ dd($__data) }}


Vru*_*aut 5

使用 Laravel Helper 函数dd

dd在刀片视图中使用:

{{ dd($__data) }} 或者 <?php dd($__data); ?>

以上两种方法都适用于刀片视图。